Pololu Robotics & Electronics
My account Comments or questions? About Pololu Contact Ordering information Distributors

Pololu Forum

Robot Arduino & ESP8266 connected to Raspberry PI


Robot Arduino & ESP8266 connected to Raspberry PI


My Robot is controlled by the Arduino MEGA2560.

Quite all the features of the MEGA2560 are used: 3 Serial interfaces, I2C, SPI, PWM, ADC and 2 interrupts. This is why I have chosen the MEGA2560 which offers such large number of interfaces. But note that MEGA2560 is a little bit limited regarding the interrupts because 4 of the 6 interrupts are not available if I2C and Serial2 are used.

The only feature missing in the MEGA2560 is a WIFI connection. For this purpose I have added 2 ESP8266. The first one manages the commands received from the user interface running on windows, the second one send data and pictures to a Raspberry PI. Why 2 ESP8266? Because sending picture takes times and 2 ESP allow sending command in parallel.

Here the global flow:

What is the purpose of this Robot? Nothing J except to discover few devices around the Arduino eco system and build a communication protocol between windows, ESP, Arduino and PI.

Seriously this robot is able to process several commands, to run independently, to detect environmental changes and then send alerts and pictures to the Raspberry PI.

For its part, the PI stores the data into text files and MySQL Database, and sends them via email.


Now let’s discuss more in detail about the devices implemented. Below the list of devices and the implementation diagram.


  • 4 motors
  • 2 H-bridges
  • 2 Contact sensors
  • Sharp IR
  • Servo for IR
  • ToF
  • Compass
  • LCD
  • RGB led
  • Buzzer
  • Camera
  • Tilt&Pan servos for Camera
  • SD Card
  • RTC
  • Brightness sensor
  • Temperature & Humidity sensor
  • Motion sensor
  • Sound sensor

First of all, there are 4 motors controlled by 2 H-bridges managed using PWM and digital pins. They have a dedicated alimentation Lipo in order to not disturb the MEGA2560 but don’t forget to connect the ground otherwise PWM will not work.

In order to allow the robot to run independently, the following devices are implemented:

  • One Time Of Flight TOF connected to I2C indicates with a good precision the distance (max 1,20 m) between the front of the robot and a potential obstacle

  • One IR Sharp connected to ADC and mount on a servo checks the distance (max 0.80m) with an obstacle around the robot (left and right). Note that it is less precise than the TOF. The servo is controlled by PWM.

  • 2 contact sensors are implemented on left and right side to detect hit, in case the robot didn’t detect the obstacle on time!

When an obstacle is detected, the robot turns to the direction where this is no obstacle or he turns back if he detects obstacle on left and right sides. A compass connected to I2C provides the direction in order to control the turn.

The robot can also check the environment and send alerts in case of abnormal changes. The parameters monitored are the following:

  • Motion is detected by a sensor that trigger an interrupt on the MEGA2560

  • Temperature & Humidity connected to Digital pin allows the detection of abnormal variation of temperature or humidity

  • Brightness is also monitored by a sensor connected to I2C

  • I have tried several sound sensors buy none of them is adequate: they don’t detect when I increase the volume of my speakers. Don’t hesitate to recommend one!

In case of alert, the robot takes pictures using a camera mount on a tilt pan. So the camera can move from -180° to + 180° horizontally and vertically. The camera is connected to Serial3 and the 2 servos of the tilt pan to PWM.

The pictures are stored into jpeg files on a SD Card connected with SPI. Then the MEGA2560 send them via Serial1 at 38400 bauds to the ESP8266 Client and then to the Raspberry PI by WIFI with the data captured by the robot

There are PHP programs on the Raspberry PI that stores in TXT files and in a MySQL database the data received. In case of alerts, the Raspberry PI sends email with these data and the corresponding pictures attached.

In addition of all these devices, there are some other components to monitor the system: RGB led and buzzer connected to digital pins, Real Time Clock RTC and LCD connected to I2C.

There are mainly used to understand what is the doing the system as there is no more connection and logging to the IDE standard Serial port when the robot runs.

In the next future, I will add the possibility to log in the SD card with a timestamp. It will be helpful to understand some strange behavior during an autonomous run.

Below the core part with the MEGA2650 and the 2 ESP8266 + some devices (LCD, Camera,Tilt&Pan, compass, motion sensor, Sd Card, RTC, brightness sensor)


This robot can run independently but he can also be managed by a User Interface UI running on Windows. This UI is written in HTML/jQuery using google widgets. As explained previously, the commands are sending to a dedicated ESP8266 Server that interrupts the MEGA2650 prior to send it via Serial2 the command. So the MEGA2650 is not blocking to wait for a command, but he can manage it as soon as needed.

The responses are transmitted back to the ESP8266 and then to the UI. Note that the biggest inconvenient with ESP8266 is the consumption, about 80mah; So the LIPO 780maH is empty after a couple of hours, so I need to switch off manually the ESP8266 Server as soon as I don’t need any more of the UI.

The next step is the implement a Bluetooth interface that is low consumption, I will keep you posted !

To avoid such constraint with the second ESP8266 Client, it goes after 2 minutes into a deep sleep mode and the MEGA2650 wakes-up it when needed by sending a pulse to its pin DTR/RTS prior sending any data or picture.

The commands are the following:

Command Purpose
Test IOT Test communication with the ESP8266 server ü
Check every n sec Check every n seconds ü
Stop Stop ü
Picture Make picture and return the picture number ü
Move Tilt&Pan Move Tilt&Pan Horizontaly and Verticaly ü
Check around Check if there is an obstacle and return the result ü
Get infos Get infos and return them
PI Communication mode Define the communication mode between the Robot and the Raspberry PI

Below the User Interface in HTML/JS&jQuery build with bootstrap and Google charts.

Communication to Raspberry PI

The second ESP8266 Client is in charge to send data and Picture to the Raspberry PI by WIFI.

In order to reduce consumption, it is always in deep sleep mode and it is wake up by a low pulse of 10 ms to its pin DTR/RTS triggered by the Arduino prior sending any data or picture via the serial interface. Note that as the ESP boot procedure uses also Serial to log some information, the startup of the ESP was sometimes blocked to I swap the Serial pin of the ESP just after the start to avoid conflict with Arduino.

As soon as the data are sent, the Arduino send a sleep command the ESP to go back in deep sleep mode.


In term of software, I have written in C++ one lib per device and there is a main program Robot that handle the core processing of the MEGA2560 and that calls the functions in the libs.

At the end the program is quiet large, hopefully the MEGA 2560 has enough memory to run it, but for sure you can’t run it on Uno.

In addition there are 2 main program for the ESP Server and the ESP Client.

Most of the code source has been found in the net (thanks to open source!) and then customized/improved. One key point is that most of the time the sources found on the net don’t handle properly the errors so I have completed this part, mainly on the I2C part.

Each device been tested alone with dedicated sketch before to be integrated to the core program. This is very useful to better understand the behavior and to know the limitation. For example, sometimes devices provide erratic measurements so it is more secure to get a sample of measures and compute the median.

The initialization part is also very critical and I have added for each device some tests cases during this phase in order to validate before starting that everything is ready.

I have also build a lib to define a protocol between ESP and Arduino to ensure a good quality of transmission on the Serial interface and to avoid deadlock. This is mandatory as the Serial interface is not secure, some bytes can be lost it the Serial buffer is full and the reader not ready. Furthermore I have increase the default Serial buffer size from 64 to 128 on the Arduino in order have the same size than on ESP. The logic of the protocol is described below:

Source code

Source code is located in 3 Git repos, the first one contains everything for Arduino and ESP:

The second one contains the source code of the User Interface:

The third one contains the source code for the Raspberry PI:



Thank you for posting in such detail and sharing your code. Your robot looks really cool!

I am sure many people will enjoy your write-up, too.



Which is the cost to assemble the electronic wirhout chassis, of a robot like this?
Can be used to equip a real model car having high speed for competitions?



the cost is les than 150 euros but for the time being ii too slow for high speed because the servo tale times to turn

i am adding 2 extra detectors to improve this part


I’miei interested to buy this kit to organize my competitions, can we cooperate to develop?