In the past few days I’ve been busy installing the brain of my Arduino Lego robot, namely the Raspberry Pi, and making a mount for the Raspberry Camera to allow it to pan and tilt. If you haven’t read the first entry of my robot blog, you can do so here.
The Raspberry Pi is running Gentoo Linux, my favourite Linux distro. Installation was straightforward using the wiki and cross-compiling the software on my Gentoo desktop. The Arduino is connected directly to the Raspberry via USB and communicates over serial. All my robot code is done in Python (except for C++ for the Arduino), but since I’m still working on that I’ll focus on the mechanical parts in this blog update.
At this point the robot is basically completed mechanically.
I’ve installed the Raspberry Pi on top of the robot with some strategically placed Lego beams and axles. It sits just clear of the Arduino and its shield underneath it. The power supply for the Raspberry is a mobile phone power pack slung underneath the robot with rubber bands. The Arduino uses a 9V battery which also powers the 2 servo motors for the camera mount. Lastly, the robot Lego motors are powered by a Lego 9V (actually 7,2V) power pack on the back.
Underside view showing the battery pack for the Raspberry Pi and the 2 Lego motors.
I’m communicating with the robot via WiFi from either my desktop PC or laptop. At this time I just connect the Raspberry to my router but I’ll probably make a hotspot on my laptop to be able to take the robot out into the field. Since communications never leave the LAN I don’t experience too much lag and can easily pilot the robot in real time.
For the camera mount I chose not to use Lego parts as it would become too bulky and I couldn’t really find a way to attach the servos to them without having to modify the Lego parts. Only the lower servo is attached to Lego, for the upper servo I made a simple frame from some spare metal and attached that to the servo plastic arms. The camera is attached to the upper servo with double-sided tape.
Due to the limitations of the servos the camera rotates only +/- 50 degrees from center (up and down). I think the servos are good up till +/- 70 degrees but I prefer to keep a margin not to damage them. As of now the camera is controlled from the PC with the arrow keys in 10 degree increments and a key to recenter it. It works well, but I’ll probably look into programming a more fluid movement, possibly using a gamepad.
My next task will be modifying the code to show the live video feed within Pygame instead of a separate VLC window, and generally modifying and cleaning up my code. Especially the networking part needs a good look through.
Whenever I have something new to show I’ll write the next entry, but expect it to be focused on the software not the hardware. Oh, and I’ll soon make a video of the robot in action!
I’ve been building an Arduino powered Lego tracked robot on and off for the past half year, and have finally come to the point where there’s something to show.
I didn’t take long to realize that I had to become proficient in programming and not just patch together other people’s code from the web, so I took a crash course in Python (using the excellent book Python Crash Course by Eric Matthes) and became familiar with C and C++ for the Arduino code.
After a few months of coding I’m finally at the point where it is all coming together. I’ve had the basic Lego robot built for months but never really programmed it to do anything other than running forward and stopping. The robot is built of parts from the Lego Crawler Crane kit (#42042), it’s basically the tracked base with some modifications like a motor for each track to allow steering. The motors are controlled via the Arduino (see my first blog entry), which in turn is controlled with a Python program on my PC via the serial monitor. The plan is to have a Raspberry Pi as the onboard PC, which in turn will be controlled via Wifi from another PC or laptop.
Back view of the robot
At the moment the robot is controlled with a simple Python program using the WASD keyboard keys, allowing forward and backward movement and turning. This works relatively well, with about a 1 second delay.
I took the video below when testing the serial communications; at that point I had only programmed forward movement.
I have attached the Arduino code below. It waits for a serial command to actuate the motors. If you intend to use this code yourself, be aware that some lines may be split because of the narrow WordPress theme format (should be fixed by now).
// Tank movement controlled via serial commands
// Works with Adafruit Motor Shield v.2.3
// Written by Kenneth Larsen, 2017
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Select ports for motors: M1 - M4.
Adafruit_DCMotor *Motor1 = AFMS.getMotor(1);
Adafruit_DCMotor *Motor2 = AFMS.getMotor(2);
String input; // input for Serial
int c = -20; // error correction
int FULLSPEED = 255;
int HALFSPEED = 126;
AFMS.begin(); // create with the default frequency of 1,6KHz
// Define forward movement
for ( i = 30 ; i < 255 ; i++ ) // accelerate slowly
Motor1->setSpeed(FULLSPEED); // maintain max speed
// Define backward movement
for ( i = 30 ; i < 255 ; i++ ) // accelerate slowly
Motor1->setSpeed(FULLSPEED); // maintain max speed
// Define left turns
// Define right turns
// Define stopping
Motor1->run(RELEASE); // stop motors
if (Serial.available() > 0)
input = Serial.readString();
if (input == "forward")
if (input == "backward")
if (input == "left")
if (input == "right")
if (input == "stop")
The Python code uses pygame to grab the keystrokes and send them to the Arduino via serial. I’ll split this program into two, one for the PC to send the commands and receive video, and another for the Raspberry Pi to forward the commands to the Arduino and send video directly from the Raspicam to the PC. This will be done over WiFi to make the robot completely cordless, and with better range than if I went with Bluetooth (and better security!).
# Serial control for Arduino robot
# Written by Kenneth Larsen, 2017
import serial, sys
# Initialize pygame
pygame.display.set_caption("Robot Control Center")
screen = pygame.display.set_mode((640,480))
# Initialize serial connection
ser = serial.Serial('/dev/ttyACM1', 115200)
ser = serial.Serial('/dev/ttyACM0', 115200)
print("No serial connection found. Is the Arduino connected?\n")
# Define stop keys. Releasing any of these keys will cause the robot to stop
stopkeys = (pygame.K_w, pygame.K_a, pygame.K_s, pygame.K_d)
"""Handles program shut down."""
print("Stopping robot and exiting...\n")
# Stop motors
# Main loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
# Keyboard controls
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
if event.key == pygame.K_w:
if event.key == pygame.K_s:
if event.key == pygame.K_a:
elif event.key == pygame.K_d:
elif event.type == pygame.KEYUP:
if event.key in stopkeys:
#if event.key == pygame.K_w or pygame.K_s or pygame.K_a or pygame.K_d:
# Update display
This is just the initial programming of the robot. I’ve tried to keep everything as simple as possible to assure that it all works.
Next step will be to get the Raspberry Pi on the robot and connect the Arduino directly to it. I’m currently experimenting with sending live video from the Raspberry over Python. The next update will hopefully focus on this, with the robot controlled over WiFi from a laptop.
I usually change a lot between speakers and headphones on my PC – Speakers when I watch movies or listen to music, and headphones when I’m at the desktop. I hate having to insert and remove the jack every time I want to change, so about a year ago I bought a cheap audio selector from Amazon. That worked for about half a year until one channel started to become lower volume than the other. So instead of just buying some new cheap selector, I decided to channel my inner maker and make my own.
I decided to go with 3 outputs so I could connect an extra pair of speakers in the future. I was able to get some 3-input 2-position switches at my local electronics store that I could just wire with 1 input and 1 output to get the desired off-on effect.
The circuit is rather simple: connect the centre of all the switches to the input jack (red wire right speaker (ring), white wire left speaker (tip), black ground (sleeve)), and one side of each switch to an output jack (the 3rd side will be empty, corresponding to OFF).
Diagram shows only 2 switches, but can easily be expanded
Having taken some quick measurements (the box had to fit below my secondary monitor) I made a back-of-the-envelope draft of the design and collected the parts. For my design I needed 4 3,5mm female audio jacks, 2 male jacks (to connect the input side of the box to the PC), 3 switches and some cable.
I decided to go with American walnut for the box as I had several sheets lying around at varying thickness. I ended up using 4 mm thick sheets for the end pieces and 2 mm for the rest. The backside was problematic, the female audio jacks I had bought could only be screwed onto a plate of maximum 1 mm thickness, and walnut is rather brittle at that thickness. Eventually I found the lid of a plastic cookie box and used that for the back side. Cheap and easy!
Construction of the box was quick and straightforward:
Creating the box
Though soldering the switches and cables allowed me to practice some new swear words (I’ve never been good at soldering).
To finish the box I sanded it with some fine grit sandpaper and gave it a coat of linseed oil. In the future I might draw numbers or symbols over each switch on the front to identify it.
Viewed from the back. The red plate is from a cookie box.
Finished audio selector
All in all, this was a fun 2-day project for me. In total I think the parts cost me some 20€; I might have been able to get a finished box cheaper, though I’ve not had much luck finding any for sale, and the few on Amazon are either very expensive or poor quality. I’m very happy with the result, the switches are nice to operate and the box is sturdy and looks good on my desk. And most importantly, you just can’t beat home-made electronics!
Working through the book Arduino Workshop by John Boxall I’ve finally come to the chapter describing how to build an Arduino Tank Robot. However, instead of buying a premade robot chassis I decided to make it out of Lego Technic instead, something I played a lot with as a child.
This presented me with some interesting challenges, first of all, how to interface the Lego Power Functions motors with the Arduino. Luckily, I’m not the first one doing this stuff, and I quickly found a great blog detailing how to cut the motor cables. As I didn’t want to permanently destroy the great Lego set I had just bought (the 42042 Crawler Crane), I bought some new cables and an extra motor from Lego.
Preparing the cables was easy: cut them, strip and tin the two outer wires for the battery pack (+9V and GND), and the two inner cables for the motors.
Cutting the cables (Lego fans should look away!)
To check if it all worked, I built a quick circuit with a TIP122 transistor according to this Arduino tutorial (though a bit simplified, I didn’t really need the switch). It worked perfectly, and I was able to control the motor speed with some simple PWM code. Next step was to run 2 motors off the Adafruit motor shield. As you can see in the video below, it all worked perfectly.
This website stores some user agent data. These data are used to provide a more personalized experience and to track your whereabouts around our website in compliance with the European General Data Protection Regulation. If you decide to opt-out of any future tracking, a cookie will be set up in your browser to remember this choice for one year. I Agree, Deny