December 30, 2015

How to sprintf a float with Arduino

Arduino has a small limitation in sprintf function that does not print correctly float numbers.
The following test sketch demonstrates the problem

void setup()
void loop()
  float f = 123.12F;
  Serial.print("Original value: ");

  char str[50];
  sprintf(str, "String value: %f", f);

You can see from the screenshot below that the float is always formatted a question mark '?'.

Using integer trick

There is a little trick to correctly print the float value using sprintf.

sprintf(str, "String value: %d.%02d", (int)f, (int)(f*100)%100);

This trick works well only with positive numbers and formats the float with two fixed decimals.

Using dtostrf function

A better solution is to use the dtostrf function. It it is more flexible and works well also with negative numbers.
These two lines of code will write a string into the buffer with strcpy function and the append the float value using the dtostrf function.

strcpy(str, "String value using dtostrf: ");
dtostrf(f, 2, 2, &str[strlen(str)]);

December 28, 2015

Installing WiFiEsp library

The easiest way of installing the WiFiEsp library in the Arduino IDE is using the Arduino Library Manager.

Launch the Arduino IDE and select Sketch - Include Library - Manage Libraries

The Arduino Library Manager will open. All you have to do is search for 'WiFiEsp' and hit the 'Install' button.

After the library has been installed you can open one of the examples.

Alternative download

If you want to manually install the library you can dowload it from GitHub and deal yourself with the installation process.

December 27, 2015

Flashing ESP8266 firmware v1.5 using Arduino Uno

I have to admit that I had some bad experience when trying to flash new firmwares to my ESP-01 modules but today I have found an easy and reliable way to flash ESP8266 firmware v1.5 (AT v0.51) using my Arduino Uno board as an FTDI controller.

You need the following items:
  • A Windows PC with Arduino IDE installed
  • An Arduino Uno board
  • A reliable connection between ESP-01 module and Arduino board. This procedure is based on my cheap Arduino WiFi shield with ESP-01


First of all you have to build my cheap Arduino WiFi shield with ESP8266. This will provide a solid foundation for powering the ESP module and setup the communication to the Arduino board.
Ensure you are able to issue simple AT commands to the ESP-01 board before proceeding.

Now modify the wiring as follows.
  • Remove ATmega328P chip from the Arduino board. By removing the microcontroller you can use the Arduino Uno onboard FTDI interface to directly communicate with the ESP module.
  • Connect ESP' RDX pin to the RX pin of Arduino (pin 0).
  • Connect ESP's TXD pin to the TX pin of Arduino (pin 1).

Test connectivity

Open the Arduino IDE, select the correct port and open the serial monitor.
You should be able to issue AT commands and get a nice output. Try 'AT' and 'AT+GMR' commands.

If you do not get any output you can try the following.
  • The serial monitor baud rate must correspond to the ESP UART baud rate. Old modules have 9600, newer ones have 115200.
  • Try different settings for the 'Line ending' option of the serial monitor. For my ESP module I have to set it to 'Both NL & CR' as you can see in the screenshot.
Try different combinations until you are able to correctly interact with the ESP module using the serial monitor.

Download firmware and tools

Download the following files and extract them

Flash it!

To put the ESP module in flashing mode you must connect ESP's GPIO0 pin to ground.
  1. Unplug the Arduino board from the PC
  2. Put a jumper between GND and GPIO0 pin (see photo)
  3. Plug the Arduino board to the PC.

Follow these steps to flash the new firmware to the ESP-01 board.
  1. Close the Arduino IDE if still opened
  2. Launch the ESP flash download tool (ESP_DOWNLOAD_TOOL_V2.4.exe) you have previously extracted
  3. Apply the following settings
  4. Bin files (from the esp_iot_sdk extracted zip file) :
    1. bin\at\noboot\eagle.flash.bin - 0x00000
    2. bin\at\noboot\eagle.irom0text.bin - 0x40000
    3. bin\blank.bin - 0xfe000
    4. bin\blank.bin - 0x7e000
  5. Flash size: 8MBit
  6. COM port: choose your Arduino COM port
  7. Baud rate: 115200 or 345600 (this is not related to the ESP baud rate

 Now press the START button and wait for the flashing process to complete.

Now press the STOP button and close the flashing tool.

Test connectivity and set default UART speed

Before proceeding it is better to verify that the new firmware is working fine.
  1. Unplug the Arduino board from the PC
  2. Remove the jumper between GND and GPIO0 pin
  3. Plug the Arduino board to the PC
Open the Arduino IDE, select the correct COM port and open the serial monitor.
Test connectivity with 'AT' and 'AT+GMR' commands. The correct settings for the ESP firmware v1.5 are:
  • Speed: 115200
  • Line ending: Both NL & CR
If you are going to use the ESP-01 module with Arduino Uno you have to lower the default baud rate because the SoftwareSerial interface maximum speed is around 38400 bps. I suggest to set the ESP module to use 9600 or 19200 bps. To set the correct baud rate use this command:

Now set the serial monitor speed to 9600 and test again the communication.

Rebuild your ESP WiFi shield

Remove the modifications made to the ESP shield.
  • Insert the ATmega328P chip back into the Arduino board
  • Connect ESP' RDX pin to Arduino pin 7
  • Connect ESP's TXD pin to Arduino pin 6

Compile and upload the updated sketch and you should be able to correctly interact with the ESP module.

Your ESP8266 board is now flashed with the updated firmware and ready to be used for your connected projects!

Don't forget to take a look at my WiFiEsp library for Arduino.

December 6, 2015

Cheap Arduino WiFi Shield with ESP8266-01

If you want to connect your Arduino with the internet the standard choice is the Arduino WiFi shield. However, this is too much expensive since it costs more than 80 USD. Cheaper alternatives based on CC3000 chipset are not meeting my expectations so I have decided to build my own Arduino WiFi shield.

My requirements for the shield are:
  • As much as cheap as possible (less than 10 USD)
  • Arduino library compatible with the standard Ethernet/WiFi libraries
  • Breadboard-friendly (no soldering needed)
The idea was simply to find a cheap replacement of the standard WiFi shield being able to reuse existing samples and code developed for Ethernet and WiFi shields.

The obvious choice for the WiFi module is the ESP8266-01. ESP8266-01 is great and cheap but there are some problems and limitations:
  1. Arduino boards are typically powered at 5V while ESP8266 needs a 3.3V power source. The Arduino 3.3V output pin cannot provide the power needed by the ESP (up to 250mA). This can be solved using a 3.3V voltage regulator like the LM1117, LD1117, LD33V or AMS1117. There are even more cheaper solutions but I decided to go with a AMS1117 module.
  2. ESP8266 connector is not breadboard-friendly. Using the technique described in this post is easy to plug the ESP into a breadboard with no additional hardware.
  3. ESP8266 default firmware uses AT commands and there is no good library for the ESP with a programming model similar to the WiFi library. There are some improved firmwares around but I want to use the stock firmware. I have developed my own EspWiFi library so I can use any existing Arduino WiFi sketch with few little code changes.
  • ESP8266-01 module (3 USD)
  • AMS1117 module - 5V To 3.3V DC-DCPower Supply (1 USD)
  • Breadboard (2 USD)
  • Capacitor (any 10-100 uF is ok)
  • 2 or 3 resistors
  • Jumper wires
  • Arduino ProtoShield (3 USD)

All these components allows you to build a WiFi shield for less than 10 USD !

Look at this Instructable for detailed instructions about how to build the ESP WiFi shield.

After having built it you can try my WiFiEsp library to connect your Arduino projects to the Internet.

December 4, 2015

How to plug the ESP8266-01 module to a breadboard

One of the most annoying problems of the ESP8266-01 module is that it is not possible to plug it into a breadboard for fast prototyping.
Today I was discussing with another Arduino fan and we were looking at existing solutions to this common problem. All the existing solutions were complex to implement or are costing more than the ESP module itself.
I ended up with a smart and free solution with no additional hardware required.

The solution is easy and requires only a small screwdriver to remove the plastic spacers and pliers to bend the ESP's pins.
Pictures are more clear than words in this case.