Motor Control with the Raspberry Pi




import RPi.GPIO as gpio
import time

gpio.setmode(gpio.BOARD)

gpio.setup(7, gpio.OUT)
gpio.setup(11, gpio.OUT)
gpio.setup(13, gpio.OUT)
gpio.setup(15, gpio.OUT)

gpio.output(7, True)
gpio.output(11, True)
gpio.output(13, True)
gpio.output(15, False)
time.sleep(0.5)

gpio.cleanup()
		

Check out the video for connecting the motors to the L298n H-Bridge.

Here's some simple code that will run one of the motors for 0.5 seconds. Notice the use of gpio.BOARD for the gpio naming conventions.

Save this script as robot1.py, then, when everything is hooked up and you're ready to roll (get it!?!?!?), do sudo python robot1.py

Try swapping around the True and False statements in the outputs, see what changes it makes.

Now that we've got one motor working, let's work on adding the other three, and figuring out how to get them to work together!

The next tutorial:





  • Robotics with the Raspberry Pi
  • Programming GPIO example
  • Running GPIO
  • Building Autonomous / RC car intro
  • Supplies needed
  • Motor Control
  • Connecting the four motors
  • Forward and Reverse
  • Turning
  • Pivoting
  • User Control
  • Remotely controlling the car
  • Adding a distance sensor (HC-SR04)
  • Programming with the distance sensor
  • Autopilot and/or auto-correct
  • Autonomous Beginnings
  • Testing Autonomous Code
  • Streaming video example one
  • Less latency streaming option