Build a Self‑Balancing Segway with Raspberry Pi
Step 1: Gather Materials
- Raspberry Pi 2 with Raspbian installed, powered by a 5 V AC‑to‑DC adapter – $45.00
- USB Wi‑Fi adapter (e.g., Edimax) – $10.00
- Two DC motors, two wheels, a lightweight car frame, acrylic sheets, motor brackets, brass studs, shaft couplings, and assorted screws – $50.00
- Motor gear ratio is 1:34; a 1:21 ratio may offer better performance if torque and speed allow.
- 12 V AC‑to‑DC adapter – $4.00
- L293D motor driver IC – $3.00
- MPU6050 6‑axis IMU – $3.00
- 830‑point breadboard – $4.00
- Jumper wires (male‑to‑male and male‑to‑female) – $3.00
- Cable ties (x8) – $1.00
- Total: ~$123.00
Step 2: Assemble the Frame
Follow the provided frame instructions to mount the motors, wheels, and electronics onto the chassis. Use the acrylic sheets to create a clean, low‑profile look.
Step 3: Wire the Circuit
Connect the MPU6050 and L293D to the Raspberry Pi and the power supply as shown below. This configuration allows the Pi to read motion data and drive the motors.
- MPU6050 VCC → Pi pin 1 (3.3 V)
- MPU6050 SDA → Pi pin 3 (SDA)
- MPU6050 SCL → Pi pin 5 (SCL)
- MPU6050 GND → Pi pin 6 (GND)
Motor driver connections:
- Left wheel: Pi pins 11, 13, 15 → L293D pins 2, 7, 1
- Right wheel: Pi pins 16, 18, 22 → L293D pins 15, 10, 9
- L293D pin 3 & 6 → left motor terminals (M+, M-)
- L293D pin 14 & 11 → right motor terminals (M+, M-)
- L293D Vs → 12 V supply
- L293D Vss → 5 V supply
- L293D pins 4, 5, 12, 13 → GND
Power the L293D with the 12 V adapter, and supply the Pi with its own 5 V adapter.
Step 4: Secure Everything
Use cable ties to clamp all wiring and components firmly in place, ensuring a tidy build that won’t shift during operation.
Step 5: Install Software
Assuming Raspbian is installed and you can SSH into the Pi, set up the I²C kernel module and the WiringPi library. These allow the Pi to communicate with the MPU6050 and control the GPIO pins.
$ sudo apt-get install libi2c-dev
Enable I²C by editing /etc/modules:
i2c-bcm2708 i2c-dev
Ensure the blacklist file /etc/modprobe.d/raspi-blacklist.conf has the following lines commented:
#blacklist spi-bcm2708 #blacklist i2c-bcm2708
Activate I²C via sudo raspi-config (Advanced Options → I²C). Reboot the Pi:
$ sudo bash; sync; sync; reboot
Verify the module loads:
$ lsmod | grep i2c
Install WiringPi:
$ sudo apt-get install git-core $ git clone https://github.com/WiringPi/WiringPi $ cd WiringPi $ sudo ./build
With the hardware and software ready, you can now program the Pi to balance the segway using the MPU6050 data.
Manufacturing process
- How to Read Temperature with a DS18B20 on Raspberry Pi 2
- Measuring Temperature on Raspberry Pi with Maxim 1‑Wire Sensors and DS2482 I2C Bridge
- Accurate Temperature Monitoring in a Server Closet with Raspberry Pi
- Lightning Detection with Raspberry Pi and AS3935: A Practical Guide
- Build a Headless Raspberry Pi 2 Water Alarm with Adafruit T‑Cobbler Plus
- Build a ThingSpeak Temperature Monitor with Raspberry Pi & BrickPi
- Build an Automated Aeroponics System with Raspberry Pi and Humidity Sensor
- Capturing IR Remote Commands on Raspberry Pi without LIRC: A Python UART Approach
- Installing a Raspberry Pi Camera in a Birdhouse – Step‑by‑Step Guide
- Build a Robot with Raspberry Pi and Python: A Complete Guide