Menu Close

NodeMCU Analog Input on Arduino IDE

This tutorial of Robo India explains the basics of input and output programming in physical computing world. This tutorial teaches how to take analog input from NodeMCU.

1. Introduction:

To read an analog signal through the NodeMCU, Analog to Digital conversion is required. A NodeMCU has 10 bit ADC which means it scales an analog signal in a range of 0-1023.

In this example an analog input is taken and it displayed on an LED and the serial monitor.In order to show the input result on LED, Mapping of input value is need. Thus mapping is done by dividing input values by 4.

This tutorial is for NodeMCU on Arduino IDE. Please note that the same tutorial can be performed on LUA as well.

S.No.ItemQuantity
1NodeMCU 1
2Breadboard 1
3LED 1
4Resistor 1k 1
5Jumper Male to male 7
6Preset 10k 1

2. Building Circuit

Schematic Diagram:

Circuit Layout:

3. Programming:

Once the circuit part is done, NodeMCU is needed to be programmed. Here is the code to run this circuit on NodeMCU.

You may download this code (Arduino Sketch) from here.

// Robo India Tutorial 
// Digital Input and Output on LED 
// Hardware: NodeMCU

const int analog_ip = A0; //Naming analog input pin
const int LED = 0;        //Naming LED Pin
int inputVal  = 0;        //Variable to store analog input values

void setup() {
  pinMode(LED, OUTPUT);  // Defining pin as output
  Serial.begin(9600);    // Initiating Serial communication

}                                                                                                                                                                                                                           
void loop() {
  inputVal = analogRead (analog_ip); // Analog Values 0 to 1023
  Serial.println (inputVal);
  analogWrite(LED, inputVal/4);      // Mapping 0 to 255
  delay(1000);
}

4. Output

The analog input is printed on the serial monitor. LED brightness is adjusted as per the analog input value. Try rotating preset and see the effect on the LED and serial monitor.

5. Troubleshooting

Input values are not changing: Check the preset connections.

If you have any query please write us at support@roboindia.com

Thanks and Regards
Content Development Team 
Robo India
https://roboindia.com

1 Comment

  1. Matt

    I’m using the NodeMCU and having trouble with the analog input.
    I’ve set up a thermistor circuit, using the 3.3V produced by the unit, which is powered from the 5V of the USB.
    The max value reads 1024, but with the thermistor removed (open) and only a resistor (1K) to ground, I should read zero. Instead I read a 7.
    I’m not sure that intermediate values are correct, but they seem sensible.
    I suspect that my reading incorrect thermistor numbers has to do with the “zero is not zero” problem.
    Any ideas?

Leave a Reply