Home Chapter 10 BS2 Photo resistors & PULSIN

Site Search

GTranslate

Chinese (Simplified) French German Italian Portuguese Russian Spanish
BS2 Photo resistors & PULSIN


In this activity, you will learn about a new part called a photo resistor. By using this part with a capacitor and a special bit of code called PULSIN, you can make a resistor/capacitor circuit (R/C circuit). This circuit allows you to use a photo resistor as a sensor to control a servomotor.

 

Photo resistor. The size of the resistor is related to its ability for variance.

 

The photo resistor is a device that varies its resistance based on a variable light environment. They utilize silicon technology and are very useful for a number of applications, Such as making an R/C circuit with the Stamp.

R/C exploit the behavior of currents and voltages that change in time. With R/C circuits, a capacitor and resistor are connected in series, and the speed that a capacitor’s is able to charge and discharge is related to the light hitting a photo resistor.

This makes the photo resistor a powerful input sensor for the Stamp. These resistors vary in the way they react to light, and they can respond differently to different frequencies of light.

This circuit can also be thought of as a simple analog to digital converter circuit as it takes analog signals and with the bit of code and BS2 translates them into digital code.

Parts Necessary

1  Photo resistor
jumper wires
1 220 ohm resistor
1 .01 mF capacitor

 

3D model of circuit

 

 

Circuit schematic

 

Programming the Sensor and Introducing the RC Time Pbasic Command:

RCTIME Pin, State, Variable
RCTIME measures the time a pin remains in a state or the charge or discharge time of R/C circuit.

  • Pin is a variable/constant/expression (0–15) that specifies which I/O pin to use. This pin will be placed into input mode.
  • State is a variable/constant/expression (0--1) that specifies the desired state to measure. Once Pin changes states, the measurement ends and stores the length of the measurement in Variable.
  • Variable is a variable (usually a word) in which the time measurement will be stored.

In the next program, you will use the Stamp to take measurements of the different values of the RCTIME constant of light, dark, and the variances in between. This would allow you to use this circuit to create a very sensitive light break beam. That is, if you have a source of light at one end of a room and you have the sensor in a tube pointing toward that source, then you can have a viewer shadow-activate the sensor.

Enter the code below

 

' {$STAMP BS2}
' {$PBASIC 2.5}

Value VAR Word

Measure: 'start of program named 'measure'
HIGH 0 'make pin 0 high and charge it to prepare for
‘discharge measurement
PAUSE 1 'this controls the charging time for the capacitor
RCTIME 0, 1, Value 'measures discharge time which is affected by resistance of
‘photo resistor
DEBUG DEC ? Value 'print value of discharge time

PAUSE 300 'allow .3 seconds for you to read value from debug screen
GOTO measure 'start program over

The next program will average out collected values of light, which you can then use to adjust other programs.

' {$STAMP BS2}

Time VAR Word

V1 VAR Word
V2 VAR Word
V3 VAR Word
Average VAR Word
Adjusted VAR Word

Bv1 VAR Word
Bv2 VAR Word
Bv3 VAR Word
average VAR Word

X VAR Word

Offset: 'start main program named 'offset'

HIGH 0 'make pin 0 high and charge it to prepare for discharge measurement
PAUSE 1 'this controls the charging time for the capacitor
RCTIME 0, 1, v1 'measure and store discharge time as variable v1

HIGH 0
PAUSE 1
RCTIME 0, 1, v2 'measure and store discharge time as variable v2

HIGH 0
PAUSE 1
RCTIME 0, 1, v3 'measure and store discharge time as variable v3

Average = (v1 + v2 + v3)/3 'average the measured values and store as 'average'
DEBUG DEC? Average 'display average

IF v1 < average THEN smalls 'if v1 is smaller than average then goto subprogram 'smalls'

Adjusted = v1 - average 'create adjusted value from the averaged offset value
DEBUG DEC? Adjusted 'display the adjusted value
GOTO B

Smalls: 'smalls' performs correction if v1 is smaller than the averaged value
Adjusted = average - v1
DEBUG DEC? Adjusted
GOTO B

B: 'start main program named 'offset'

FOR time= 500 TO 1000 'start a repeating cycle that counts up from 500 to 1000
PULSOUT 12, time 'send loop counter info stored in 'time' to servo
PAUSE 10 'pausing for 50 instead of 10 moves the servo extra slowly
NEXT

HIGH 0 'make pin 0 high and charge it to prepare for discharge measurement
PAUSE 1 'this controls the charging time for the capacitor
RCTIME 0, 1, bv1 'measure and store discharge time as variable v1

HIGH 0
PAUSE 1
RCTIME 0, 1, bv2 'measure and store discharge time as variable v2

HIGH 0
PAUSE 1
RCTIME 0, 1, bv3 'measure and store discharge time as variable v3

Beverage = (bv1 + bv2 + bv3)/3 'average the measured values and store as 'average'
DEBUG DEC? Beverage 'display average

 

Measure

This photo resistor can now be combined with the servomotor to exhibit some intelligent behavior. See the figure below of this combined circuit. By holding your hand over the sensor, you can affect the movement of the servomotor based on variable resistance. You must very directly block the photo resistor with a shadow or a piece of paper for more accurate positioning.

 

3D model of circuit

 

Programming the project

' {$STAMP BS2}
' {$PBASIC 2.5}

collect VAR Word
adjusted VAR Word
X VAR Word

 

Program: 'start of program loop

HIGH 0 'hold pin 0 high to charge capacitor
PAUSE 1 'charge capacitor for 1/1000 of a second
RCTIME 0, 1, collect 'measure discharge time of photo resistor through capacitor

Adjusted = collect*2 'apply algebraic transformation to make the measured value usable for servo control

 

FOR x = 1 TO 100 'start a repeating cycle that counts up from 500 to 1000
PULSOUT 12,adjusted 'send loop counter info stored in 'time' to servo
PAUSE 10 'pausing for 50 instead of 10 moves the servo extra slowly
NEXT

GOTO program 'start at beginning of program loop