Analog input: Here we have the ability to measure how much voltage is present at the pin. Not every pin on a typical microprocessor can do this. The voltage must be between zero and the supply voltage (3.3 volts on ESP32). The pin uses a circuit called an ADC or Analog to Digital Converter that "measures" the voltage and converts it into a value . The value produced depends on how many bits the processor uses. An Arduino uses 10 bits (values from 0 to 1023. The ESP32 uses 12 bits (values from 0 to 4095). 1023 is 11 1111 1111 or 10 ones in binary, 4095 is 1111 1111 1111 or 12 ones in binary. Here's a review of binary.
The analogRead(pin) command is used. Like digitalRead, we need to set some other variable equal to the value this command generates. myVoltage = analogRead(pin). We can use this when we want finer control over a process. For instance, we can use a rotary variable resistor and analogRead to control how fast we spin a motor.
In order to try this, we need some voltages to measure. If you have a variable resistor you could follow this lab demo on Random Nerd Tutorials. Your kit does not have a variable resistor (sometimes called 'pot' - short for potentiometer). We can try making a variable resistor using the graphite from a pencil. If we create a thick black line on paper by laying down a thick continuous layer of graphite we might get a very small amount of current to flow through. Graphite has a very high resistance - just be careful that the + and ground leads do not touch directly.
I drew mine like dumbbells' 5 cm long near the fold of a sheet of paper folded in half for strength. I took a jumper from ground and taped the other end to the right hand bell. I took another jumper from 3.3V and taped it to the left hand bell. A third jumper is held in hand and connected to GPIO15. Make sure the 3.3V and ground jumpers do not touch.
/*
esp32 AnalogReadSerial
Taken from the built in AnalogReadSerial example
http://www.arduino.cc/en/Tutorial/AnalogReadSerial
*/
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
void loop() {
// read the input on GPIO pin 15
float sensorValue = analogRead(15); //line A
// float sensorValue = (analogRead(15)/4095) * 3.3; //line B
// print out the value you read:
Serial.println(sensorValue);
delay(250); // delay in between reads for stability
}
SAFETY GLASSES ON!
The code, to the left, is taken from one of the Arduino built in examples. Plug in your USB cable. Load the code and open the Serial Monitor.
Click on Tools ==> Serial Monitor
Make sure the baud rate (lower right) is set to 9600.
As you scratch the GPIO15 jumper along your pencil line you should see the numbers change. As you get closer to the left side the numbers should go up. As you get closer to the right side the numbers should go down.
NOTE: This may take a few tries. I had to add more pencil graphite after every attempt. Make sure the connections at the bells are firm.
When the jumper touches the + wire connected to 3.3V you should get a reading of 4095. When the jumper touches the ground wire you should see 0V.
If you "rem-out" line A (put // in front) and remove the comment slashes or "rems" from line B you will get the actual voltage levels.
How would you change the Serial.print to show that the reading is in volts?
Unplug your USB cable.