Category Archives: ESP8266

Serial.print and printf, Solved! in Arduino IDE and ESP

Serial.print and printf, Solved! in Arduino IDE and ESP. You’d like to be able to print values from inside strings, similar to printf. Since the Arduino IDE understands serial.print, you won’t be able to type anything for Arduino Uno, Arduino Mega or Arduino Nano etc like the statement below:

Serial.printf("You Entered %d hours", n);

However, there is a trick you can use, which is similar to many languages support it, such as php. See the code below.

Serial.print and printf, Solved! in Arduino

You simply need to specify a string, which can be blank or empty, and then begin printing using serial.println starting with the string variable, followed by a + sign to Concatenate with a constant string under double-course (” “), followed by another + sign, followed by the variable name, which can be int or float type, and so on.

int x = 36;
float y = 1.02;
String str;
void setup() {
  Serial.begin(9600);
  Serial.println(str + "Int: " + x + ", Float: " + y);
  delay(1000);
}

void loop() {

}

OUTPUT

Serial.print and printf, Solved
Serial.print and printf, Solved

Serial.printf in ESP8266

Serial.printf does not work with the Arduino boards like Uno, Mega, or Nano boards, but it does with the ESP8266 or NodeMCU boards. See the code below

int x = 36;
float y = 1.2;
void setup() {
  Serial.begin(115200);
  Serial.printf("Int is: %d and Float is %f \n", x, y);
  Serial.printf("Int is: %d and Float is %.2f \n", x, y);
  delay(1000);
}

void loop() {

}

Where,

Serial.printf("Int is: %d and Float is %.2f \n", x, y);

%.2f restrict a float number to just two digits after the decimal point in Arduino IDE.

OUTPUT

printf in nodemcu esp8266 or esp32

Upload .hex file in Arduino using CMD prompt and .bin to ESP8266

Here’s how to use the CMD prompt to upload a .hex file (compiled hex file) to Arduino without using the Arduino IDE. Also, how to use the command prompt to upload a .bin file to an ESP8266 or ESP32.

If you’re not sure what .hex and .bin files are, read on. The code written in the Arduino IDE is written in C and/or C++, a high-level programming language that is easy for us to read and write. Microcontrollers are incapable of understanding high-level programming languages.

However, when you click upload, the Arduino IDE converts your code into hex or/and bin files, which are then uploaded to microcontrollers. In general these files act as your device’s firmware.

Many times, a developer would make .hex or .bin file accessible to a client because the developer put in a lot of effort to write that code and doesn’t want to show you how he did it, so the developer can just provide you with a compiled .hex or .bin file instead of programming code, and you would need to learn these steps to upload .hex and .bin file using command prompt (CMD prompt) without using Arduino IDE.

HOW TO CREATE A .hex OR .bin FILE

Select your ‘development board’ under “Tools,” then click on ‘Export compiled binary’ under “Sketch.”

    • Tools → Board: → Arduino Genuino/Uno (let)

      Select Board Arduino Genuino Uno
      Select Board Arduino Genuino/Uno
    • Sketch → Export compiled Binary

      Export Compiled Binary Arduino
      Export Compiled Binary Arduino

This will generate two hex files if you’re using an Arduino board, or bin files if you’re using an ESP8266 or ESP32 board, in the same folder (same location) where the ino file is saved.

HOW TO UPLOAD .hex FILE IN ARDUINO UNO/NANO/MEGA USING COMMAND PROMPT

Before you begin, keep in mind that you will need three things

    • avrdude location
    • avrdude.conf location
    • .hex file location
STEP 1

To begin open the Arduino IDE and go to File > Preferences, check the box upload under Show verbose output during:

Show verbose output during compilation and upload Arduino IDE
Show verbose output during compilation and upload – Arduino IDE

STEP 2

Select a board from the tools menu, in my case Arduino Genuino/Uno, and a COM port from the tools menu. Mine is COM14, but it’s possible that yours is different.

Select COM port (Communication Port)
Select COM port

STEP 3

If everything is in order, you can upload any code or sketch (either blank or any)

STEP 4

Before the red colour text began from, something like “avrdude: Version 6.3-20171130” as seen in the screenshot, after the upload was successful.

Find hex file upload command
Find hex file upload command

You’ll come across avr dude command like this, but your path may be slightly different. Have a look at these points and see if they contain -CC, -Uflash:w:\path_of_hex_file:i

C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avrdude -CC:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf -v -patmega328p -carduino -PCOM14 -b115200 -D -Uflash:w:D:\MyCode\String_Seprator\String_Seprator.ino.standard.hex:i

Copy this line and paste into some text editor like notepad++, next 2 steps are important.

STEP 5

Replace the path between ‘w’ and ‘i’ in the last red line with the path to the hex file you intend to upload.

STEP 6

Since the CMD prompt doesn’t recognize spaces between paths see here in Program Files (x86), use double-course “” for the path This is what the command above would look like. (see below).

"C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avrdude" "-CC:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf" -v -patmega328p -carduino -PCOM14 -b115200 -D -Uflash:w:"D:\PIJA Education\String_Seprator\String_Seprator.ino.standard.hex:i"

If you don’t use double-course, you’ll get an error like this:

C Program not recognized as an internal or external command
‘C:\Program’ is not recognized as an internal or external command

‘C:\Program’ is not recognized as an internal or external command, operable program or batch file.

STEP 7

Now open a command prompt (Windows+r), type cmd in the run wizard, and click OK.

Run window - cmd
Run window – cmd

STEP 8

The path from step 6 should be copied, right-clicked, and pasted; the shortcut key ctrl + v would not work here in cmd prompt.

STEP 9

If all of the steps above are completed correctly, you can see that the upload was successful. with a message at end “avrdude done.  Thank you.

Upload hex file in Arduino using Command (cmd) prompt
Upload hex file in Arduino using cmd prompt

HOW TO UPLOAD .bin FILE IN ESP8266 USING COMMAND PROMPT

To upload .bin file to ESP8266, follow the same instructions from 1 to 3 as we did for Arduino, except board and port selection, these will be different different this time. For example board may be  NodeMCU 0.9 (ESP-12 Module) and Port is COM11.

STEP 1

Once you upload code successfully, Before the red colour text began from, something like “esptool.py v2.8” as seen in the screenshot

Find NodeMCU/ESP8266 bin file upload command
Find NodeMCU/ESP8266 bin file upload command

You’ll come across upload command like this, but your path may be slightly different.

C:\Users\abc\AppData\Local\Arduino15\packages\esp8266\tools\python3\3.7.2-post1/python3 C:\Users\abc\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4/tools/upload.py --chip esp8266 --port COM11 --baud 115200 --before default_reset --after hard_reset write_flash 0x0 C:\Users\abc\AppData\Local\Temp\arduino_build_9655/test.ino.bin

Copy this line and paste into some text editor like notepad++, next 2 steps are important.

STEP 2

Replace the path in the last red line with the path to the bin file you intend to upload.

STEP 3

Since the CMD prompt doesn’t recognize spaces between paths see here in Program Files (x86), use double-course “” for the path, if space present in path. This is what the command above would look like. (see below).

C:\Users\abc\AppData\Local\Arduino15\packages\esp8266\tools\python3\3.7.2-post1/python3 C:\Users\abc\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4/tools/upload.py --chip esp8266 --port COM11 --baud 115200 --before default_reset --after hard_reset write_flash 0x0 "D:\PIJA Education\test/test.ino.nodemcu.bin"
STEP 4

Now open a command prompt (Windows+r), type cmd in the run wizard, and click OK.

Run window - cmd
Run window – cmd
STEP 5

The path from step 3 should be copiedright-clicked, and pasted; the shortcut key ctrl + v would not work here in cmd prompt.

STEP 6

If all of the steps above are completed correctly, then click enter and you can see that the upload was successful with a message at end “Hard resetting  via RTS pin…

HOW TO UPLOAD .bin FILE IN ESP8266 USING COMMAND PROMPT
Upload bin file in ESP8266 using Command (cmd) prompt

 

 

 

EEPROM IN ARDUINO AND ESP (ESP8266 and ESP32)

This article will explain what an EEPROM is and provide a brief overview of the other memories available in a microcontroller. You”ll also learn how to use EEPROM in Arduino and ESP microcontrollers (ESP8266 and ESP32).

Using the EEPROM put and EEPROM get functions, the software below illustrates how to read and write to EEPROM.

EEPROM IN ARDUINO AND ESP

MEMORY USED IN MICROCONTROLLER

There are various types of memories, including Flash memory, cache memory, RAM, and EEPROM.

Continue reading EEPROM IN ARDUINO AND ESP (ESP8266 and ESP32)