Discover this podcast and so much more

Podcasts are free to enjoy without a subscription. We also offer ebooks, audiobooks, and so much more from just $11.99/month.

How to read voltages with analogRead()

UNLIMITED

How to read voltages with analogRead()

FromLearn Programming and Electronics with Arduino


UNLIMITED

How to read voltages with analogRead()

FromLearn Programming and Electronics with Arduino

ratings:
Length:
15 minutes
Released:
Mar 12, 2017
Format:
Podcast episode

Description

In the last lesson you learned about using the analogRead() function to collect data from a sensor connected to one of the Arduino analog pins. The range of data we received from the analogRead() function was mapped between 0 to 1023. What if we want to know the actual voltage being applied at the pin? If you like this tutorial, click here to check out FREE Video Arduino course – thousands of people have really enjoyed it. You Will Need Potentiometer (any resistance range will work) Jumper Wires – at least 3 Persian Rug Step-by-Step Instructions Place the potentiometer into your breadboard. Run a jumper wire from the 5-Volt pin of the Arduino to either one of the outside pins of the potentiometer. Run another jumper wire from one of the ground pins on the Arduino (labeled GND) to the other outside pin of the potentiometer. Run the final jumper wire from pin A0 on the Arduino to the middle pin of the potentiometer. Plug the Arduino into your computer. Open up the Arduino IDE. Open the sketch for this section. Click the Verify button on the top left side of the screen. It will turn orange and then back to blue once it has finished. Click the Upload button (next to the Verify button). It will turn orange and then back to blue once it has finished. On the menu bar, go to Tools > Serial Monitor – this will open the Serial Monitor window – you should see numbers rolling down this screen. Now adjust the knob of your potentiometer and watch the serial monitor window, the numbers should adjust between 0 and 5. Adruino Board Set up For Switch Case This image crafted with Fritzing. The Arduino Code /* ReadAnalogVoltage Reads an analog input on pin 0, converts it to voltage, and 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); // 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); } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 /* ReadAnalogVoltage Reads an analog input on pin 0, converts it to voltage, and 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); // 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); } Discuss the Sketch This sketch does the exact same thing as the last lesson sketch except for one important change. It takes the reading provided by the analogRead() function and converts it into the actual voltage value at the respective analog pin. Let’s start from the top to review what is taking place. We have no variables to declare and initialize at the beginning of the sketch so we jump right into the setup() function. Inside the curly braces of setup() we begin serial communications by setting the baud rate. This is done using the function Serial.begin(9600). void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); } 1 2
Released:
Mar 12, 2017
Format:
Podcast episode

Titles in the series (61)

Video lessons on learning programming and electronics with Arduino. This is part of our Arduino Crash Course and Arduino Course for Absolute Beginners. It's designed to take someone with little or no experience in programming and electronics and get them fast-tracked to learning the skills to prototype using Arduino. We'll include some lessons from the first edition and the second edition of our training course.