Command a DC motor with Raspberry Pi
I did it! I've been wanting to do this for a while and finally got down to doing it. It's definitely not hard, but still, it takes a bit of effort if you don't have the background.
Here's my set-up:
For those not familiar with circuit symbols,
The two vertical lines = power supply (these are the pins from the Raspberry Pi)
The three horizontal lines = ground (also to the Raspberry Pi)
The circle surrounded by 'C', 'B' and 'E' = NPN transistor
The circle with an 'M' inside = motor (that's my DC motor)
The triangle with a line at its tip = diode
All I wanted to do was command the DC motor to either move or not move, via one of the GPIO pins. That's simple enough. There's a number of GPIOs on the RPi that provide 3.3V and can be flipped on and off over a simple Python program. This GPIO provides current to the base of an NPN transistor, and that allows current to flow from the collector to emitter of the transistor.
The voltage that drives the motor is from the constant 5V pin of the RPi.
Here's the result!
All the program does is flip the 3.3V pin ON for two seconds, then OFF for two seconds, ad infinitum. Note that the (normally off) push button is in series with this 3.3V pin, which means current flows if and only if the button is pressed.
References:
Sidenote: I've titled this post "command" a DC motor rather than "control" a DC motor because to me, control implied some kind of feedback loop, which is definitely not what's happening here.
Here's my set-up:
Circuit diagram to command a DC motor with Raspberry Pi |
For those not familiar with circuit symbols,
The two vertical lines = power supply (these are the pins from the Raspberry Pi)
The three horizontal lines = ground (also to the Raspberry Pi)
The circle surrounded by 'C', 'B' and 'E' = NPN transistor
The circle with an 'M' inside = motor (that's my DC motor)
The triangle with a line at its tip = diode
All I wanted to do was command the DC motor to either move or not move, via one of the GPIO pins. That's simple enough. There's a number of GPIOs on the RPi that provide 3.3V and can be flipped on and off over a simple Python program. This GPIO provides current to the base of an NPN transistor, and that allows current to flow from the collector to emitter of the transistor.
The voltage that drives the motor is from the constant 5V pin of the RPi.
Here's the result!
All the program does is flip the 3.3V pin ON for two seconds, then OFF for two seconds, ad infinitum. Note that the (normally off) push button is in series with this 3.3V pin, which means current flows if and only if the button is pressed.
References:
- Flyback diode [wikipedia] the diode that eliminates voltage spikes when the DC motor is turned off
- Controlling a DC motor with RPi [YouTube] my circuit is pretty much copied from this guy
- GPIO reference [adafruit] what each RPi pin does
Sidenote: I've titled this post "command" a DC motor rather than "control" a DC motor because to me, control implied some kind of feedback loop, which is definitely not what's happening here.