Intro
Figure 1. The electrical-electronics and embedded hardware required to build the object detection DC motor controller.
Project Parts List
- Raspberry Pi (Model A+,B, B+, or the Pi 2)
- (Q1) 2N2222 NPN transistor or equivalent
- (R1) 220 ohm resistor (red, red, brown, gold), 1/4W, 5%
- (D1) 1N4001 rectifier diode or equivalent
- (M1) 3-6VDC motor or equivalent
- (Pi 1) Raspberry Pi Cobbler or equivalent
- (R2) photocell
- solderless breadboard
- DMM (digital multimeter) or VOM (volt-ohm-milliammeter)
- jumper wires (hand stripped 22 AWG [American Wire Gauge]) solid wires or Adafruit Breadboard wires Product ID: 153)
- (VCC1) 6VDC Battery pack
- 1.5VDC AAA or AA Batteries x 4
- (S1) tactile pushbutton switch
Light Detection and the Photocell
Figure 2. The electrical symbol and component view of a photocell
Figure 3. The authors ohmmeter is reading a value of 3.11Kilo-ohms. Note: Resistance values will vary based on the ambient light received by the photocell.
Next, adjust the scale to the highest megaohm setting. Place your hand over the photocell and you will see a resistance value in a few millions of ohms as shown in Figure 4.
Figure 4. The author's ohmmeter is reading a value of 1.587 megaohms.
Figure 5. The author's homebrew collimator built from an ink pen finger grip.
Building a Simple Light Sensor Switch
Figure 6. A simple light sensor switch built using a photocell and a pull-up resistor. Note: The pull-up resistor is programmed electrical component using a single line of Python code.
Build the Transistor Motor Driver Circuit
Figure 7. The transistor motor driver solderless breadboard wiring diagram
Figure 8. The electronic circuit schematic diagram of a typical transistor DC motor driver
Basic Transistor Theory
The collector and emitter leads are negative (N-material) with the base being positive (P-Material). Figure 9 shows the pin configuration and package type for the 2N2222 NPN transistor. As shown in Figure 8, the base is attached to the +6VDC battery pack positive-red wire through a tactile pushbutton switch and a 220 ohm series limiting resistor with the emitter wired to ground (the black wire). A 220 ohm resistor is attached to the transistor's base to reduce the full battery current and heat from damaging it. The 220 ohm resistor is a nice resistance value to allow a sufficient amount of base current to flow so the transistor will turn on properly. Also, the 1N4001 diode prevents back peak reverse current from the DC motor winding damaging the transistor when it turns off. If you're interested in learning more about this semiconductor device, read AAC's article on Bipolar Junction Transistors for additional electrical theory information. Let's proceed to the final hardware build of the project.
Figure 9. The electronic symbol and component package type for the 2N2222 NPN transistor
Final Hardware Build
With the transistor motor driver circuit working properly, we are now ready to complete the final hardware build for the project. The final electrical wiring for the object detection/DC motor controller is to wire the driver circuit to the Raspberry Pi. In the LED flasher project, the Adafruit Pi cobbler was used to connect the opto-isolator and supporting circuit components to the appropriate RPi's GPIO pins. The Pi Cobbler provides convenience in terms of having all the RPi GPIO pins accessible on the solderless breadboard for electrical wiring to electronic interfacing circuits. Therefore, the same circuit wire assembly technique will be used in this project as well. Figure 10 shows the solderless breadboard wiring for the object detection DC motor controller. Again, the electronic circuit schematic diagram shown in Figure 11 is provided as an additional wiring resource.
Figure 10.The complete object detection DC motor controller wiring diagram assembled on a solderless breadboard. Notice
the removal of the tactile pushbutton switch, +6VDC battery pack and
new placement location of the electronic components on the solderless
breadboard.
Figure 11. The electronic circuit schematic diagram for the object detection DC motor controller
Figure 12. Final
build of the author's the object detection DC motor controller. The
2N2222 transistor has enough Beta to properly drive the Meccano erector
motor without overheating it.
The Object Detection Python Code
GPIO.setup(photocell_pin, GPIO.IN, pull_up_down = GPIO.PUD_UP)
The entire Python program can be typed onto the LXTerminal by opening the nano editor with the linux command ~sudo nano object detection.py. Also, the python program can be saved on your RPi's SD card by clicking the code button below.
# ***********Object Detection code******************
#
# inspired by Simon Monk, Raspberry Pi Cookbook, 2013
#
# modified by Don Wilcher Dec 18, 2015
#
# Placing a object over the photocell will turn on the dc motor.
# Placing an object over the photocell a 2nd time turns off the motor.
# Add libraries to python script
import RPi.GPIO as GPIO
import time
# Include BCM I/O pins into python script and define pin numbers
GPIO.setmode(GPIO.BCM)
photocell_pin = 4
motor_pin = 18
# Create photocell pin as an active low switch (use RPi internal pullup resistor)
# and define motor pin as an output.
GPIO.setup(photocell_pin, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(motor_pin, GPIO.OUT)
# Define and set (initialize) the motor output state as False and the old input event as True
motor_state = False
old_input_event = True
# pbswitch event monitoring loop: check pbswitch_pin and toggle dc motor output based on input events
# being True or False
while True:
new_input_event = GPIO.input(photocell_pin)
if new_input_event == False and old_input_event == True:
motor_state = not motor_state
old_input_event = new_input_event
GPIO.output(motor_pin, motor_state)
time.sleep(0.1)#provides a 100 msec motor actuation on/off time
With the objection detection Python code entered into the LX Terminal, type the Linux command ~sudo python object_detection.py after the prompt onto the screen. Take an object and place it over the photocell: the DC motor should be spinning. Place the same object over the LDR to turn off the motor. A big congratulations is in order! You now have an operational object detection DC motor controller. An application for an object detection device is a non-contact start switch for a conveyor system. Instead of using a standard pushbutton switch to start the conveyor, a basic hand motion will operate it. Explore other applications with your object detection DC motor controller and record them in a lab notebook. Next time, we'll investigate how to build an event counter using the RPi along with litteBits modules.
No comments:
Post a Comment