To allow the motor to be controlled from your BS2, you can simply turn the transistor on and off by programming the pin to go high and low. Enter the program below ' {$STAMP BS2} ' {$PBASIC 2.5}
OUTPUT 11 Turn_on: HIGH 11 PAUSE 500 LOW 11 PAUSE 500 GOTO turn_on You will notice that this drives the motor to be on at full speed and then off for a period of time, conditioned by the pause cycle. What if you wanted to control the speed of the motor? The next program will show you how to use the TIP 120 and PWM (pulse width modulation) to speed up and slow down the motor. PWM code makes a pin output high at 5 volts and then low at 0 volts for as many cycles as you want. When you turn the pin on and off quickly, you create an output from your BS2 that is high (5v) half the time and low (0v) half the time. Using PWM you can create an average voltage that varies between 0 and 5 volts. If you are at 5 volts for 50% of the time and at 0 volts 50% of the time, then the average voltage would be 2.5 volts over time. Well, what if you wanted an average voltage of 1 volt for a slower speed? PWM syntax: PWM, Pin, Duty, Cycles • PWM converts a stream of digital signals to analog output via pulse-width modulation. • Pin is a variable or a constant expression between 0 and 15 (the pin numbers). • Duty is a variable, constant, or expression between 0 and 255 that determines the output level between 0 to 5 volts. • Cycle is a variable, constant, or expression between 0 and 255 that determines the length of time the PWM signal is on. PWM sends a burst of ones and zeros (5 and 0 volts at output) and the ratio is proportional to the duty you specify. The proportion of ones to zeros in PWM is then called the duty cycle. In the next program, you will be using PWM to start the motor off slowly and then to speed it up and slow it down again. Enter the program below and hit run. Type in the code below '{$STAMP BS2} ' {$PBASIC 2.5}
OUTPUT 11 'specifies PIN 11 as an OUTPUT LOW 11 'disables the motor by turning PIN 11 TO 0 volts DC X VAR Word 'creates a variable called x Start: FOR x = 0 TO 255 STEP 5 'speed up motor by increasing PWM DUTY CYCLE by 5% each cycle PWM 11,x, (255-x) 'using (255-x) to decrease PWM DURATION accelerates FOR…NEXT loop ‘cycling NEXT FOR x = 255 TO 0 STEP 5 'speed up motor by increasing PWM DUTY CYCLE by 5% each cycle PWM 11,x, (255-x) 'using (255-x) to decrease PWM DURATION accelerates FOR…NEXT loop ‘cycling NEXT LOW 11 'turn motor off PAUSE 100 GOTO start In this next lesson, you will be using a PIR (passive infrared sensor) to sense your infrared heat. This sensor will then allow you to use the PIR to turn the motor on from a distance of 1 to 8 feet. The PIR or passive infrared sensor take a minute to warm up. It images the infrared on your body or another infrared source and then only a chance in this infrared quantity will allow the sensor Parts Required
1 TIP 120 Darlington transistor (Radioshack Catalog #: 276-2068) 1 DC motor either a gearhead motor or a small DC motor scrapped from a surplus machine.
If you are using surplus motor make sure you are dealing with a DC motor. This application will not work with a servo or stepper motor or low power AC motor. Refer to chapter 16 on Motors and Muscles for further information on the differences between these motors.
1 12-volt power supply for motor power. 1 1N4001 diode (Radioshack Catalog #: 276-1101) 1 Ik ohm resistor 1 K76 PIR sensor (PIR module KC7783 or KC778B) Google to find best prices Building the Circuit
Leaving the TIP 120 and motor driving circuit in place, add the PIR circuit below. | 3D model of circuit | | Schematic of circuit |
- Place PIR in the board as above.
- Be very careful to have the sensor polarities correct for power and ground. You may notice on the back of the sensor in small writing where the 3 wires meet the board that it says V+, OUT and GND.
- Wire in the 10k ohm resistor between the ground and the out pin of the PIR. Connect the ground pin of the PIR to the board’s ground with a connector wire.
- Connect the V+ pin of the PIR to VDD on the board.
- Connect the OUT pin of the PIR to pin 0 on the board.
Programming the Project
In this program you will learn to program the PIR sensor to activate the motor. You can have the motor spin for as long as you like, but by leaving the motor on for 80 milliseconds, the system is a bit more responsive. This sensor is designed to be activated by infrared in the frequency of human body heat, but you can also activate this sensor by putting it in break-beam configuration, as in Chapter 9. One way to increase the range of the sensor is to point the sensor toward a light so that when even a non-infrared oriented object passes in front of the sensor, it will be activated. Remember, it takes some time for the PIR to warm up.
PROGRAMMING A PASSIVE INFRARED SENSOR Type in the code below ' {$STAMP BS2} ' {$PBASIC 2.5}
INPUT 0 ‘define 0 as input and 11 as output. OUTPUT 11 Sense: IF (IN0 = 1) THEN move 'look to see if PIR has detected and if yes goto move GOTO sense 'if pin 0 is not high return to start of program Move: a label for the motor subroutine 'PIR will stay high for 1 second after activated HIGH 11 ‘turn motor on PAUSE 80 ‘pause for 80 milliseconds LOW 11 ‘turn motor off GOTO sense
|