Hey future robot makers! 🚗✨
You’ve already learned the basics of Python: input(), print(), if, and even time.sleep(). You’ve also connected your Raspberry Pi to a motor driver with GPIO pins. Guess what? You’re now just a few lines of code away from moving your robot car forward and backward! Let’s do it!

robo

🧠 What You’ll Learn

  • How to control GPIO pins using Python.
  • How to write functions for forward() and backward().
  • How to use input() to trigger motion.
  • And a little challenge at the end just for you 😉

🔌 Quick Recap: Wiring the Robot Car

Before we code, make sure:

  • You connected the GPIO pins from the Raspberry Pi to the IN1, IN2, IN3, IN4 pins on the motor driver.
  • The motor driver is powered properly (through battery or external supply).
  • You know which GPIO pins control which motor.

Let’s assume:

  • Left Motor: IN1 (GPIO17), IN2 (GPIO18)
  • Right Motor: IN3 (GPIO22), IN4 (GPIO23)

🐍 Step 1: Setup Your Python File

Start your Python file with the following setup:

import RPi.GPIO as GPIO
import time

# Set the pin mode
GPIO.setmode(GPIO.BCM)

# Motor pins
IN1 = 17  # Left Motor Forward
IN2 = 18  # Left Motor Backward
IN3 = 22  # Right Motor Forward
IN4 = 23  # Right Motor Backward

# Setup all pins as output
GPIO.setup(IN1, GPIO.OUT)
GPIO.setup(IN2, GPIO.OUT)
GPIO.setup(IN3, GPIO.OUT)
GPIO.setup(IN4, GPIO.OUT)

🕹️ Step 2: Write the Forward and Backward Functions

Let’s make the car go forward for 2 seconds!

def forward():
    print("Going forward")
    GPIO.output(IN1, GPIO.HIGH)
    GPIO.output(IN2, GPIO.LOW)
    GPIO.output(IN3, GPIO.HIGH)
    GPIO.output(IN4, GPIO.LOW)
    time.sleep(2)
    stop()

Now for backward:

def backward():
    print("Going backward")
    GPIO.output(IN1, GPIO.LOW)
    GPIO.output(IN2, GPIO.HIGH)
    GPIO.output(IN3, GPIO.LOW)
    GPIO.output(IN4, GPIO.HIGH)
    time.sleep(2)
    stop()

🚩 Step 3: Don’t Forget to Stop!

We need to stop the car after it moves:

def stop():
    GPIO.output(IN1, GPIO.LOW)
    GPIO.output(IN2, GPIO.LOW)
    GPIO.output(IN3, GPIO.LOW)
    GPIO.output(IN4, GPIO.LOW)
    print("Stopped")

🎯 Step 4: Ask the User for Input

Let’s add a simple controller:

while True:
    command = input("Enter command (f = forward, b = backward, q = quit): ")

    if command == "f":
        forward()
    elif command == "b":
        backward()
    elif command == "q":
        print("Quitting")
        break
    else:
        print("Invalid command")

🧹 Step 5: Clean Up After Yourself

At the very end, always clean up the GPIO pins:

GPIO.cleanup()

🔥 Challenge for You

You’ve got forward and backward working. Can you write the left() and right() functions on your own?
(Hint: Left motor goes slower or stops when turning right… and vice versa.)


✅ Final Code

If you combine everything, your full code will look like this:

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

IN1 = 17
IN2 = 18
IN3 = 22
IN4 = 23

GPIO.setup(IN1, GPIO.OUT)
GPIO.setup(IN2, GPIO.OUT)
GPIO.setup(IN3, GPIO.OUT)
GPIO.setup(IN4, GPIO.OUT)

def forward():
    print("Going forward")
    GPIO.output(IN1, GPIO.HIGH)
    GPIO.output(IN2, GPIO.LOW)
    GPIO.output(IN3, GPIO.HIGH)
    GPIO.output(IN4, GPIO.LOW)
    time.sleep(2)
    stop()

def backward():
    print("Going backward")
    GPIO.output(IN1, GPIO.LOW)
    GPIO.output(IN2, GPIO.HIGH)
    GPIO.output(IN3, GPIO.LOW)
    GPIO.output(IN4, GPIO.HIGH)
    time.sleep(2)
    stop()

def stop():
    GPIO.output(IN1, GPIO.LOW)
    GPIO.output(IN2, GPIO.LOW)
    GPIO.output(IN3, GPIO.LOW)
    GPIO.output(IN4, GPIO.LOW)
    print("Stopped")

while True:
    command = input("Enter command (f = forward, b = backward, q = quit): ")
    if command == "f":
        forward()
    elif command == "b":
        backward()
    elif command == "q":
        print("Exiting...")
        break
    else:
        print("Unknown command")

GPIO.cleanup()

💬 Final Words

You’re now controlling a robot using real Python code and real hardware. That’s no small feat! 🎉
Now go ahead and add your own twists: left, right, maybe even turbo mode?

Next mission: make the robot dance 💃🕺
Until then, happy coding!

robo