Raspberry Pi Object Detection DC Motor Controller for Beginners
2017-02-16 | By All About Circuits
License: See Original Project Raspberry Pi
Courtesy of All About Circuits
Recommended Reading:
Build a Raspberry Pi Pushbutton Switch
Photocells also called light dependent resistors (LDRs) are often used to make lights automatically turn on at night. Most nightlights have photocells that detect darkness based on a change in their resistance. When the photocell detects darkness, the light turns on. We can use the same principle from nightlights to control a DC motor. You can see the block diagram below.
The electrical-electronics and embedded hardware needed to build the object detection DC motor controller.
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 Photocells
Photocells are light sensitive resistors and are sometimes referred to as light dependent resistors (LDRs).
The electrical symbol for a photocell and an actual photocell
The photocell's electrical response to light is to change resistance. The more light that the photocell receives on its surface, the more its resistance decreases, down to just a few hundred ohms. The photocell’s resistance is in the megaohms when it’s in the dark. You can conduct a simple experiment to see how the photocell's resistance changes based on varying light levels. First, you’ll need a digital multimeter (DMM) and set it to read ohms. Then, adjust the ohmmeter scale to the 20 kilo ohm setting. The DMM's positive and negative test leads need to be placed across the photocell like in the picture below. The ohmmeter will display a reading in a few thousands of ohms.
The ohmmeter reads a value of 3.11Kilo-ohms. Note: Ambient light can affect the resistance values.
Now, try adjusting the scale to the highest megaohm setting. Cover the photocell with your hand and you’ll see a resistance value in a few millions of ohms as shown below.
Reading a value of 1.587 megaohms.
You can make a collimator to narrow the light your resistor receives, this will cut down on the variation caused by ambient light. A simple way to make was is to cut the barrel of an ink and paint the inside black. I made my collimator from the "finger grip" of an ink pen. The reading shown in the picture above were taken with a collimator on my LDR. These minimum and maximum values will be used when we wire our light sensor switches to our RPis.
My collimator
Building a Simple Light Sensor Switch
We’ll use the photocell and a pull-up resistor to make a simple light sensor switch. The voltage drop is proportional to the amount of photocell resistance. For example, placing something over the photocell should provide an approximate 3,3VDC voltage drop across the LDR. Removing whatever you use to cover the LDR will reduce the photocell's resistance and provide a proportional small voltage drop across it. The voltage drop is read by the RPi’s GPIO pin. The Python code reads the presence or absence of light as either 0VDC or 3.3VDC. In the next phase of this project, we’ll build and test a transistor-operated DC motor. You can see the circuit in the diagram below.
Building a Transistor Motor Driver Circuit
You can use a single bipolar junction transistor (BJT) with an appropriate amplification factor (Beta) to operate a small DC motor. You can build a transistor DC motor driver easily using a breadboard. Once your circuit is built, the small DC motor wired to the transistor's collector lead should turn on when you press the tactile pushbutton switch. If the DC motor doesn't spin, you’ll need remove the battery pack from the circuit and check your wiring and the orientation of the 1N4001 rectifier diode and the 2N2222 NPN transistor. Once your wiring errors have been corrected, reconnect the battery pack and test the circuit again.
A typical transistor DC motor driver
Final Hardware Build
Now that the transistor motor circuit is working, we’re ready to complete the final hardware build for this project. The last electrical wiring task for our object detection/DC motor controller is to wire the driver circuit to our Raspberry Pi. I used an Adafruit Pi cobbler for this. The Pi Cobbler is convenient because it makes all the RPi GPIO pins accessible on a breadboard for electrical wiring to electronic interfacing circuits. The schematic is shown below.
The final build
Object Detection Python Code
The final step for our object detection DC motor controller is to add the Python code! The Python code below is a program that was reused from an LED flasher project. The names of the variables were changed to reflect the components wired to the RPi. The code tells the DC motor to turn on when an object is placed over the photocell. Placing an object over the photocell again will turn the motor off. The pull-up resistor is a programmed component that uses this line of Python code:
GPIO.setup(photocell_pin, GPIO.IN, pull_up_down = GPIO.PUD_UP)
You can type the entire Python program into the LXTerminal by opening the nano editor with this linux command: ~sudo nano object detection.py. You can download the python program to your RPi’s SD card from All About Circuits.
# ***********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
Once you’ve entered the objection detection code into the LX Terminal, type the Linux command ~sudo python object_detection.py after the prompt onto the screen. Place something over the photocell to see if it works. Your DC motor should be spinning. Place the same object over the LDR to turn the motor off. You now have a base for motion sensor activated applications with a DC motor!
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.
Visit TechForum