ESP8266 Arduino Core Interface ADC

ADC

Image

Required

  • Required Hardware – ESP8266 with Programmer (or)  NodeMCU Dev Kit
  • Required Software Tools  – Arduino IDE with ESP8266 Core

Circuit of ESP8266 Arduino Core ADC

Image

Code

/* 
  http://www.ArunEworld.com/Embedded/ESPressif/ESP8266/ESP8266_Arduino-Core/
  Tested By  : Arun(20170219)
  Example Name : AEW_ADC-Interface.ino
 */
 /*
  AnalogReadSerial
  Reads an analog input on pin 0, prints the result to the serial monitor.
  Graphical representation is available using serial plotter (Tools > Serial Plotter menu)
  Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

  This example code is in the public domain.
*/

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // print out the value you read:
  Serial.println(sensorValue);
  delay(1);        // delay in between reads for stability
}

 

Result of ESP8266 Arduino Core ADC

3
2
3
4
3
3
4
3
2
3
4
2
2

Code Explanation – AEW_ADC-Interface.ino

1. Header Comment

/* 
  http://www.ArunEworld.com/Embedded/ESPressif/ESP8266/ESP8266_Arduino-Core/
  Tested By  : Arun(20170219)
  Example Name : AEW_ADC-Interface.ino
 */
  • This is just documentation:
    • Source/author’s site
    • Date of testing (2017-02-19)
    • Example name

2. Example Purpose

/*
  AnalogReadSerial
  Reads an analog input on pin 0, prints the result to the serial monitor.
  Graphical representation is available using serial plotter (Tools > Serial Plotter menu)
  Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

  This example code is in the public domain.
*/
  • It explains what the sketch does:
    • Reads analog values from pin A0 (the ESP8266 has only one ADC input).
    • Sends the value to the Serial Monitor / Serial Plotter.
    • Suggests using a potentiometer as input (center pin → A0, sides → 5V & GND).
    • Declares that this example is free to use (public domain).

3. Setup Function

void setup() {
  Serial.begin(9600);
}
  • setup() runs only once when the ESP8266 boots or resets.
  • Serial.begin(9600); initializes serial communication at 9600 baud rate.
  • This allows the ESP8266 to send ADC readings to your PC via USB.

4. Loop Function

void loop() {
  int sensorValue = analogRead(A0);
  Serial.println(sensorValue);
  delay(1);
}
  • loop() runs continuously after setup.

Step by step:

  1. analogRead(A0);
    • Reads the voltage at pin A0.
    • The ESP8266 ADC converts this voltage (0–1V in NodeMCU, though some boards have onboard voltage dividers to allow 0–3.3V).
    • Returns an integer value (range: 0–1023).
  2. Serial.println(sensorValue);
    • Prints the value to the Serial Monitor (so you can see numbers like 0–1023).
    • Or you can visualize it as a waveform in the Serial Plotter.
  3. delay(1);
    • Adds a 1 ms delay to stabilize readings and avoid overwhelming the serial output.

What It Does in Real Life

  • If you connect a potentiometer:
    • Rotating the knob changes voltage at A0.
    • Serial Monitor shows values changing from 0 (0V) to 1023 (~1V or ~3.3V depending on board).
  • Useful for testing sensors like LDR, temperature sensors, etc., since many provide analog outputs.

 

Please turn AdBlock off, and continue learning

Notice for AdBlock users

Please turn AdBlock off
Index

Discover more from ArunEworld

Subscribe now to keep reading and get access to the full archive.

Continue reading