Wireless Remote Control Tank Robot | Raspberry Pi | Beginners




In this tutorial, we will learn how to control a tank robot remotely and wirelessly with Raspberry Pi. We will remotely connect to the robot via VNC. This is a great tutorial for beginners to Raspberry Pi. This tutorial will show you how to use the L298N Motor driver, DC motor control, GPIO pins, remote access and wireless motor control with Raspberry Pi. We will control the tank or robot from the computer, keyboard, smartphone and keypad using the curses library. --------------------------------------------------------------------- Required Hardwares Raspberry Pi 3 Model B -- https://goo.gl/fA5rXZ L298N Motor Driver -- https://goo.gl/1Wd8vy 18650 3.7v Li-Ion + Charger -- https://goo.gl/en2fyC 18650 Battery Holder -- https://goo.gl/B7DX8D Xiaomi Power Bank -- https://goo.gl/xFVmg9 Jumper Wires 3 in 1 -- https://goo.gl/EGTafY Devastator Tank Robot -- https://goo.gl/HqarpY Banggood Shopping -- https://goo.gl/dZvYUZ DFRobot Shopping -- https://goo.gl/CqyKjA --------------------------------------------------------------------- Get Started with Raspberry Pi (Installing Raspbian) https://youtu.be/X0EAPSZE5Nc --------------------------------------------------------------------- About GPIO -- https://goo.gl/izyWgz Updating and Upgrading Raspian -- https://goo.gl/N5Ew1U About VNC (Virtual Network Computing) -- https://goo.gl/m2QUS1 Download VNC -- https://www.realvnc.com/en/ ---------------------------------------------------------------------
Source Code
# import curses and GPIO
import curses
import RPi.GPIO as GPIO

GPIO.setwarnings(False)

#set GPIO numbering mode and define output pins
GPIO.setmode(GPIO.BOARD)

motor1a = 7
motor1b = 11
motor1e = 22

motor2a = 13
motor2b = 16
motor2e = 15

GPIO.setup(motor1a,GPIO.OUT)
GPIO.setup(motor1b,GPIO.OUT)
GPIO.setup(motor1e,GPIO.OUT)
GPIO.setup(motor2a,GPIO.OUT)
GPIO.setup(motor2b,GPIO.OUT)
GPIO.setup(motor2e,GPIO.OUT)

# Get the curses window, turn off echoing of keyboard to screen, turn on
# instant (no waiting) key response, and use special values for cursor keys
screen = curses.initscr()
curses.noecho()  
curses.cbreak()
curses.halfdelay(3)
screen.keypad(True)

try:
        while True:   
            char = screen.getch()
            if char == ord('q'):
                break
            elif char == curses.KEY_UP:
                GPIO.output(motor1a,GPIO.HIGH)
                GPIO.output(motor1b,GPIO.LOW)
                GPIO.output(motor1e,GPIO.HIGH)
                GPIO.output(motor2a,GPIO.HIGH)
  GPIO.output(motor2b,GPIO.LOW)
  GPIO.output(motor2e,GPIO.HIGH)
            elif char == curses.KEY_DOWN:
                GPIO.output(motor1a,GPIO.LOW)
                GPIO.output(motor1b,GPIO.HIGH)
                GPIO.output(motor1e,GPIO.HIGH)
                GPIO.output(motor2a,GPIO.LOW)
  GPIO.output(motor2b,GPIO.HIGH)
  GPIO.output(motor2e,GPIO.HIGH)
            elif char == curses.KEY_RIGHT:
                GPIO.output(motor1a,GPIO.HIGH)
                GPIO.output(motor1b,GPIO.LOW)
                GPIO.output(motor1e,GPIO.HIGH)
                GPIO.output(motor2a,GPIO.LOW)
  GPIO.output(motor2b,GPIO.HIGH)
  GPIO.output(motor2e,GPIO.HIGH)
            elif char == curses.KEY_LEFT:
                GPIO.output(motor1a,GPIO.LOW)
                GPIO.output(motor1b,GPIO.HIGH)
                GPIO.output(motor1e,GPIO.HIGH)
                GPIO.output(motor2a,GPIO.HIGH)
  GPIO.output(motor2b,GPIO.LOW)
  GPIO.output(motor2e,GPIO.HIGH)
            elif char == 10:
                GPIO.output(motor1a,GPIO.LOW)
                GPIO.output(motor1b,GPIO.LOW)
                GPIO.output(motor1e,GPIO.LOW)
                GPIO.output(motor2a,GPIO.LOW)
  GPIO.output(motor2b,GPIO.LOW)
  GPIO.output(motor2e,GPIO.LOW)
             
finally:
    #Close down curses properly, inc turn echo back on!
    curses.nocbreak(); screen.keypad(0); curses.echo()
    curses.endwin()
    GPIO.cleanup()
    
To install the NCurses library on a Debian based machine you can issue the apt-get command below:
sudo apt-get install libncurses5-dev libncursesw5-dev