Small H Bridge on a Pi

This is the code I tested with for the H Bridge on the Pi. You can either turn on GPIO pins for full speed, or you can use PWM. Pi support software PWM, and this was for non hardware PWM.

For variable using PWM variableName= GPIO.PWM(pin#, HERTZ)

To Start PWM variableName(Percentage of Hertz). Stop with variablename(0)

import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)

GPIO.setup(35, GPIO.OUT)
GPIO.setup(36, GPIO.OUT)
GPIO.setup(37, GPIO.OUT)
GPIO.setup(38, GPIO.OUT)

r = GPIO.PWM(35,100)
l = GPIO.PWM(37,100)

#GPIO.output(35, GPIO.HIGH)
r.start(15)
GPIO.output(36, GPIO.LOW)
l.start(50)
#GPIO.output(37, GPIO.HIGH)
GPIO.output(38, GPIO.LOW)

time.sleep(5)

#GPIO.output(35, GPIO.HIGH)
r.start(50)
GPIO.output(36, GPIO.LOW)
l.start(15)
#GPIO.output(37, GPIO.HIGH)
GPIO.output(38, GPIO.LOW)

time.sleep(5)

l.start(0)
r.start(0)
#GPIO.output(35, GPIO.LOW)
GPIO.output(36, GPIO.LOW)
GPIO.output(37, GPIO.LOW)
GPIO.output(38, GPIO.LOW)

time.sleep(2)
GPIO.output(35, GPIO.LOW)
GPIO.output(36, GPIO.HIGH)
GPIO.output(37, GPIO.LOW)
GPIO.output(38, GPIO.HIGH)

time.sleep(5)

GPIO.output(35, GPIO.LOW)
GPIO.output(36, GPIO.LOW)
GPIO.output(37, GPIO.LOW)
GPIO.output(38, GPIO.LOW)

Be the first to comment

Leave a Reply