Analog Read

bulb, 4k wallpaper 1920x1080, beautiful wallpaper-5665770.jpg

In this lesson we will learn how to read an analog Voltage in two different situations and learn about it`s different functions . We will use this function to read the voltage value in a setup very similar to our digital Read setup and also we will use that knowledge to detect the lines with our line sensor.


Estimated time: 10 minutes


You will learn about:

Analog Read

 Integer Data Type

 Float Data Type

Line Sensor


Components Needed

Main Boardarduino, electronic, card-5170681.jpgUSB Cable
Male to Male wireLine Sensor
Connections
Step1

Connect [|] For that, you need to place the Arduino on top of the Arduino pin headers [|], the USB port of it facing the edge of the board, and while you can see the access pins of the pinheader at both sides of the Arduino board, then push it in place to ensure the board is in place.] the Arduino to the Main board.

arduino, electronic, card-5170681.jpg
Step2:

Plug in the USB cable[|] the USB type B to Micro USB, which you can get from a cellphone charger also] to the Micro USB port of Arduino

usb, logo, input-1773302.jpg
Step3:

Upload [|] Follow this] the following code to the Arduino.[This code can be found in Files/Examples/01Basics/ReadAnalogVoltage in Arduino IDE]

// 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);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (5.0 / 1023.0);
  // print out the value you read:
  Serial.println(voltage);
}

Step 4:

Connect the male to male wire between access pin [|]A0 of Arduino and EN pin of the Bluetooth header [|] .

Step 5:

While the Arduino is plugged in, open the Serial Monitor [|] and make sure the Baud Rate [|] is set to 9600.

Step 6:

With switching the BT MD switch beside the bluetooth header, you can see the numbers become around 0 and 3.3 volts. That`s the values that pin A0 measures.

Code Explanation
  • In lines 1, 3, 7, 9, 11 and 13, [|] we have a line that starts with ‘//’ , any line with that at the start, doesn`t get read by the microcontroller, we use them for commenting or explaining or turning one line ON and OFF.] .
  • In line 2,[|] we define the start of the Void setup part of our code which runs only once when we turn the robot ON.] .
  • In line 4,[|] we begin a serial connection in order to interact with our arduino while it’s working and we define our Baud rate as 9600, that’s why we have to make sure in Serial Monitor , the Baud Rate is also 9600.] .
  • In line 5,[|] we close the void setup part of the code .
  • In line 8,[|] we start our void loop part of the code , which runs repeatedly in a loop as long as we power our Arduino.] .
  • In line 10, [|] we define an integer value [|] named sensorValue which stores the analog values [|] read in pin A0. Note that we didn’t set a different name for the pin and called it directly by it’s name as ‘A0’.
  • In line 11,[|] we start our void loop part of the code , which runs repeatedly in a loop as long as we power our Arduino.] .
  • In line 12, [|]
  • In line 14,[|] we display in Serial Connection, to be shown in Serial Monitor, the value of our voltage ,showing one value in every line, using Serial.println function .] .
  • In line 15,[|] we define the end of the Void loop part of our code.] .
Further Experimentation
  • Try adding a number in the parenthesis in line 14 where we print our float value like Serial.println(voltage); and see how the numbers will be shown. that will display the float numbers with more digits and accuracy.
  • Try adding a delay line before line 15 like delay(100); and see how that affects the performance.

  • Don’t change the circuit, add one of the line sensor modules to the circuit ,installing that in LS1 header. Upload the following code:
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(A7);
  
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (5.0 / 1023.0);
  // print out the value you read:
  Serial.println(voltage);
}
  • Notice how the value changes when you move the line sensor on a paper and compare the results on black and white surfaces.


  • Now again without changing the circuit, upload the following code:
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 A0:
  int sensorValue1 = analogRead(A0);
  delay(5);
    // read the input on analog pin A7:
    int sensorValue2 = analogRead(A7);

  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage1 = sensorValue1 * (5.0 / 1023.0);
    float voltage2 = sensorValue2 * (5.0 / 1023.0);

  // print out the value you read:
  Serial.print(voltage1);
    Serial.println(voltage2);
}

  • See how each value changes, turning the BT MD switch ON/OFF and while moving the sensor over a paper.
Practical Usage For our Projects
  • We can use analog Read function to read any analog value of our sensors, like line sensors .
  • Being able to read multiple analog values is the barebone of our line follower robots, which comparing thoser values will give us an idea of whether we are on the line or outside and adjusting our movements accordingly.
  • Using the analog read function we can read the voltage of the batteries to have an idea of how much charge they have left.
Instagram
Facebook
LinkedIn
YouTube
X (Twitter)
Scroll to Top