A practical guide to building a simple robot with microcontrollers
Arduino and Raspberry Pi are two of the most popular tools in the world of educational robotics. While Arduino is ideal for controlling hardware (motors, sensors), Raspberry Pi adds the ability to do complex data processing and connect to the internet. In this guide we'll show you how to use both to build a simple robot.
Components you'll need
Before you start, gather these basic items:
For Arduino
- An Arduino Uno/Nano board.
- Motors with wheels (e.g. a geared DC motor).
- A motor driver (e.g. L298N).
- A distance sensor (e.g. the HC-SR04 ultrasonic sensor).
- A battery (9V or LiPo).
- Jumper wires and a breadboard.
For Raspberry Pi
- A Raspberry Pi (e.g. Model 4B).
- A motor shield (e.g. L293D) or GPIO driver.
- A camera (e.g. the Raspberry Pi Camera Module).
- Sensors (e.g. an infrared sensor).
- An SD card with an OS installed (Raspberry Pi OS).
Step 1: Choosing between Arduino and Raspberry Pi
- Arduino is better suited for:
- Simple real-time projects (e.g. obstacle avoidance).
- Controlling motors and sensors without complex code.
- Raspberry Pi is better for:
- Robots with computer vision or artificial intelligence.
- Projects that need an internet connection or multitasking.
Step 2: Building a robot with Arduino
- Connecting the motors
- Connect the DC motors to the L298N driver.
- Connect the driver to the Arduino:
- IN1 and IN2 to digital pins (e.g. 5 and 6).
- ENABLE to a PWM pin (for speed).
- Adding an obstacle sensor
- Connect the HC-SR04 sensor:
- VCC to 5V, GND to ground.
- TRIG to pin 9, ECHO to pin 10.
3. Programming the Arduino
Upload this code, which makes the robot avoid obstacles:
#include <Ultrasonic.h>
Ultrasonic ultrasonic(9, 10);
void setup() {
pinMode(5, OUTPUT); // IN1
pinMode(6, OUTPUT); // IN2
}
void loop() {
int distance = ultrasonic.read();
if (distance > 20) {
// Move forward
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
} else {
// Turn right
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
delay(500);
}
}Step 3: Building a robot with Raspberry Pi
- Installing the OS and setup
- Install Raspberry Pi OS using Raspberry Pi Imager.
- Enable GPIO and the camera via raspi-config.
- Connecting the motors
- Use a motor shield such as the L293D:
- Connect GPIO pins 17 and 18 to the shield's inputs.
- Connect the motors to the outputs.
- Writing a Python script
Create a file robot.py with this code:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT) # IN1
GPIO.setup(18, GPIO.OUT) # IN2
try:
while True:
# Move forward
GPIO.output(17, True)
GPIO.output(18, False)
time.sleep(2)
# Stop
GPIO.output(17, False)
GPIO.output(18, False)
time.sleep(1)
except KeyboardInterrupt:
GPIO.cleanup()Step 4: Combining Arduino and Raspberry Pi
For more complex projects you can use them together:
- The Raspberry Pi processes data (e.g. images) and sends commands to the Arduino.
- The Arduino controls the motors and sensors.
- Connect them via USB or GPIO/UART for communication.
Step 5: Common problems and solutions
- The motors don't move: Check the connections and power supply.
- The sensor doesn't work: Make sure it's connected to the right pins.
- The Raspberry Pi overheats: Use heatsinks or a fan.
Upgrade ideas
- Add a camera for object recognition with OpenCV.
- Integrate a Bluetooth/WiFi module for remote control.
- Make the robot autonomous with algorithms such as SLAM (Simultaneous Localization and Mapping).
Arduino and Raspberry Pi are ideal for getting into the world of robotics. Start with a simple project and gradually add more complex features.
Useful resources:
On the Artec Robotics “News” page you will find a variety of articles and guides on robotics and automation. Here you'll find useful information about the latest technologies, software solutions and programs. They will help you dive deeper into the world of industrial robots and automated systems.
Follow the news from Artec Robotics!
MAKE AN INQUIRY
