Notice that the GPIO numbers for the pins don’t relate to the physical pins on the GPIO header, so take care to map them correctly when wiring and programming. The transistor acts as a simple switch, and when the GPIO pin is a zero volts, the collector to emitter is effectively an open-circuit, not allowing any current to flow through the motor. However, when the GPIO pin is 3.3v, the current can then flow from collector to emitter, switching on the motor. Also, stick in a resistor to reduce the amount of current drawn from the GPIO pin. 1K should be fine.
To enable the GPIO pins, I used gpio-admin, a small program to export the gpio pins for use. It’s available here: https://github.com/quick2wire/quick2wire-gpio-admin
Script to enable and toggle GPIO pin:
#!/bin/sh # gpio-admin export 4 cat /sys/devices/virtual/gpio/gpio4/valuee cho out > /sys/devices/virtual/gpio/gpio4/direction echo 0 > /sys/devices/virtual/gpio/gpio4/value
while [ 1 ] do echo "Turning motor on..." echo 1 > /sys/devices/virtual/gpio/gpio4/value sleep 2 echo "Turning motor off..." echo 0 > /sys/devices/virtual/gpio/gpio4/value sleep 2 done
Here’s a video of the setup in operation:
The above circuit will only drive the motor in one direction. The motor I have will go in either direction, but I need to switch the polarity of the 12V power supply. I might get to that in the near future.
Raspberry Pi is a trademark of the Raspberry Pi Foundation