How to use Bluetooth, WiFi, and Software for driving a mini rover

Communications Plan


Many how to articles on the internet explain how to light up LED lights using a micro controller such as a Raspberry Pi. That is a fun project. The next step in programming micro controllers might be running a motor, either a stepper motor or a DC motor. Inspiration quickly turns to attaching wheels and a chassis to make a mobile robot. I believe the term, robot, is not really accurate for that kind of project. A "micro controller with wheels" would be more accurate. Since there is a programmable micro controller, it is more similar to a computer guided autonomous rover. Or at least has the potential to be. So, I decided to call this project , mini rover.

Every remote control vehicle needs a good controller. This project uses an Xbox Wireless Controller.

There are tutorials for linking an Xbox Wireless Controller directly to a Raspberry Pi via Bluetooth custom adapter from Microsoft. To keep things simple and to get a better range for the mini rover, I used a WiFi workaround instead.

Bluetooth 2, consumer grade Bluetooth, has roughly a 10 meter (or 33 foot) range. WiFi communications can range from 46 meters (or 150 feet) indoor, to 92 meters (or 300 feet) outdoor. Radio would range from an indoor 30 meters (or 100 feet) to an outdoor 90 meters (or 300 feet). A WiFi network is sufficient for running a small mini rover without having to get a license for higher power Radio transmission.

A network socket via WiFi can be used for getting data from a main computer to the micro controller. The game controller transmits data to the main computer over Bluetooth, and then to the micro controller via WiFi. Given the WiFi adapter connection is secured with a radio id and password, and the controller is paired via Bluetooth, there is some basic level of security for the data. The mini rover travel distance is limited to the range it can travel within the WiFi router connection. It is not limited to the distance the game controller has with the main computer over Bluetooth.

The basic diagram for communication is as follows.

NetworkDiagram.png

  • A main computer running Windows 10 connects to a WiFi router
  • An Xbox Wireless Controller connects via Bluetooth to the main computer
  • A Raspberry Pi connects to the WiFi router
  • A socket Client application on the main computer receives game controller data and transmits parsed data to a socket Server application running on the Raspberry Pi
  • A socket Server application receives data from the Client and communicates controller data to drive the motors
  • The computer application also can use SSH to run specific Raspberry Pi commands such as start and stop of the socket Server

 

Things needed for this project


Source Code:

  • The source code for this project is located here

Electronic Parts:

  • Raspberry Pi 1 B/B+ or newer (must have WiFi either using a dongle or on board)
  • Portable 5V micro USB power supply for Raspberry Pi power : example on Amazon
    • Use the smallest and correct power charger for your Raspberry Pi. If using Raspberry Pi 4 a different USB-C power source is required. example on Amazon
  • L298N Motor Driver Board: example on Amazon
  • Battery holder for 6 AA batteries with standard snap connector for motor driver board power
    • 9V battery snap connector with bare leads
    • 6 AA batteries
  • Jumper wires for making board connections: male/male, female/male, female/female: example on Amazon

Controller:

Hardware:

Software Utilities:

Software Languages:

  • C# (.Net Core 3.1)
  • Python

Operating Systems:

  • Windows 10
  • Raspian Buster Lite

 

Prep the Pi


For the Raspberry Pi setup, I followed instructions for installing Raspian Buster Lite from here. The instructions include download links to operating system images that you flash onto a microSD card using balenaEtcher. Before running the operating system for the first time on the Raspberry Pi, it might be helpful to follow instructions for headless SSH and WiFi configuration described here. That process may shorten the time needed to get the Raspberry Pi connected to the network.

Use PuTTY to connect without a monitor, keyboard, or mouse to the Raspberry Pi. The tutorial for that is here. Note that the most important prerequisite for using PuTTY is to locate the IP address of the Raspberry Pi for the WiFi connection. See the link for different ways on locating the Pi's IP Address.

If you are using an older Raspberry Pi that does not have on board WiFi, a USB WiFi dongle is required. Setup for that is custom and I will not go into detail on it in this post.

After boot up, and once connected to the internet, it is always recommended to run the following commands to be sure your Raspberry Pi is up to date.

sudo apt update 
sudo apt upgrade 
sudo reboot

There are two libraries to install on the Raspberry Pi. One is for socket communication and the other is for GPIO PWM motor drive commands. Run the following installation commands using SSH and a PuTTY terminal for the Raspberry Pi.

sudo apt-get install socket 
sudo apt install python3-gpiozero

With those steps complete, that is all that is required for setting up the Raspberry Pi. Now it is time to go over the Python application code.

 

Electronics wire up


WiringDiagram.png

  • This project uses a Raspberry Pi 1 Model B. Which has been discontinued. Newer models of Raspberry Pi can be used for this project.

  • The wiring diagram image shows how the power sources are connected, how the motors are connected, and how the motor controller connects to the Raspberry Pi.

  • This table lists how wire are connected between the L298N and Raspberry Pi boards:

    Board Green Blue Yellow Blue Yellow Green
    L298N ENA IN1 IN2 IN3 IN4 ENB
    Raspberry Pi 1 Model B GPIO 14 GPIO 23 GPIO 18 GPIO 8 GPIO 7 GPIO 25

 

Test the controller


  • Use VS Code to open the .Net Core solution
  • Enable Bluetooth on the computer
  • Pair the Xbox Wireless Controller with the computer
  • Start the debugger configuration for the "gameController" console app
  • The game controller output data in the Debug Console.
  • All controller buttons are accounted for and some data should appear as you move the left and right joysticks.
  • Note: The mini rover will only be using data from the left joystick.

 

Test the motors


  • Remove the wheels from the motors or elevate them such that they won't travel. Also, without wheels a small piece of painters tape on the motor shafts can help you see motor direction and speed. (Make sure the tape is easily removable)
  • Power up the Raspberry Pi and the L298N board
  • Copy the python test script file called testPWM.py to the Raspberry Pi using PuTTY
  • Run the script
    python3 testPWM.py
  • The test app will cycle through each combination of motor direction and speed

 

Socket Server Tutorial


A very detailed post for working with and understanding the Python socket library is here. The basic socket server code from the article was used for this project.

 

Test the motors with the controller


  • Copy socketServer.py to the Pi using PuTTY
  • Edit the socketServer.py to use the IP Address of the Raspberry Pi
  • Use VS Code to edit the Program.cs file of the console app
    • Set the IP address of the Raspberry Pi in the client C# code. It is located in a variable near the top of the Program.cs file.
  • Start the debugger configuration for the "console" console app
  • The C# application will do the following:
    • SSH to the Raspberry Pi
    • Start the socketServer.py script via SSH
    • Enable a socket connection from the C# client to the Python server
    • Listen for a connected Xbox Wireless Controller
    • Send controller data from the client to the server
    • Ending the console normally will stop the socket server application on the Raspberry Pi
      • If the C# app is stopped, an error will occur in the socket server code that stops the script process
  • As you push or pull on the left stick of the controller, the motors should spin
  • The two motors will spin at different speeds and directions depending on what direction the left stick is pushed
  • The two motors will stop when the left stick is not pushed and is centered
  • There is no deceleration action on the controller other than reducing the amount that the left stick is pushed from center
  • Acceleration occurs the more the left stick is pushed in a direction

 

Build the mini rover


This step is entirely dependent on the kit you have chosen to work with. Follow the instructions for kit assembly and keep in mind where you will install the Raspberry Pi, L298N, and the two power sources. This will require some creativity. I've found that velcro is a good way to hold down power sources, and you can find machine nuts and bolts as well as nylon spacers to affix the micro controller and motor driver boards. (This might require drilling holes if your kit doesn't have them already.) Getting spare machine nuts, bolts, and spacers from the hardware store is time consuming so be sure to plan your trip as specifically as you can. Here is the online video tutorial for assembling the Peewee Runt Rover from Actobotics: peewee assembly link. Note that there is no detail about installing electronics to it.

 

Drive


With the two power supplies fully charged, power on the Raspberry Pi, the L298N, connect the Xbox Wireless Controller, and run the console app. If everything goes well, the mini rover will be able to drive from one room to the next where you can't see it and it will hit a wall. Congratulations!

Now you can add sensors and more code to make it autonomous. I'll leave you to it!


Disclaimer: Please be aware that by using content presented here, you consent to this disclaimer and agree to its terms. All the information on this post is published in good faith and for general information purpose only. TsugaSoft.com and its author make no warranties about the completeness, reliability and accuracy of this information. Any action you take upon the information you find on this website (TsugaSoft.com), is strictly at your own risk. TsugaSoft.com and its author will not be liable for any losses and/or damages in connection with the use of the website. You can visit other websites by following hyperlinks to external sites listed in this post. These links to other websites do not imply a recommendation for all the content found on these sites. Site owners and content may change without notice and may occur before the opportunity to remove a link which no longer works. By using the content presented here you agree that you are responsible for your own safety and understand the limits of yourself and your equipment. Children should be supervised by an adult.