-1

I am using Raspberry Pi 3 B+ and I want to run a 5 volt DC motor with RPi.GPIO, I have written a code:

import RPi.GPIO as GPIO
from pip._vendor.distlib.compat import raw_input

u = raw_input('how many volt: ')
GPIO.setmode(GPIO.BCM)

GPIO.setup(32, True)

while u == '5':
    GPIO.output(32, GPIO.HIGH)

When I run this code Error an Showing:

Warning (from warnings module):
  File "/home/pi/Documents/5v.py", line 8
    GPIO.setup(32, GPIO.OUT)
RuntimeWarning: This channel is already in use, continuing anyway.  Use GPIO.setwarnings(False) to disable warnings.

When I fix this Error:

import RPi.GPIO as GPIO
from pip._vendor.distlib.compat import raw_input

u = raw_input('how many volt: ')
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

GPIO.setup(32, GPIO.OUT)


while u == '5':
    GPIO.output(32, True)

Error is not coming but the problem is motor is not running. Can anyone help me, I am using Python2.7.15 .

1

The GPIO operate at 3.3V and can safely source 16mA which is unlikely to run a motor - even then you need additional components to protect the Pi from back EMF.

You can control a motor using a transistor or FET See https://elinux.org/RPi_GPIO_Interface_Circuits

1

The Raspberry Pi has two 5V pins but they're always on. There are also 3V3 pins which are always on, but the ones marked 'GPIO' on pinout diagrams (e.g. https://pinout.xyz - or type pinout at the command line) are variable 3V3 - you can configure them

To control a 5V supply with a GPIO pin, use a transistor. The way a transistor works is when it receives input (say 3V3 from a GPIO pin), it outputs another voltage (provided by another source). There's a decent explanation of how that works here: https://projects.raspberrypi.org/en/projects/balloon-pi-tay-popper/7

I would also suggest you use GPIO Zero instead of RPi.GPIO. You'll find it much simpler to read, write and understand. Use the OutputDevice class - shown two steps on in the above link. See https://gpiozero.readthedocs.io for more info.

  • This answer provides good advice, but the "explanation" of transistor function is seriously misleading. Bipolar transistors are current controlled devices and do not "output" anything - they control current flow (which can, of course, be in a higher voltage circuit). – Milliways Jan 18 at 3:04
  • Absolutely - I have edited accordingly – ben_nuttall Jan 18 at 13:16
0

Search for a TXB0104 level shifter. It is a bi-directional level shifter that can translate +3.3v pulses to +5.0v pulses. I use it for a +5v stepper motor control. https://www.sparkfun.com/products/11771

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.