Tutorial 09: How to read voltages with analogRead(): Arduino Course for Absolute Beginners (ReM)

preview_player
Показать описание
🤩 FREE Arduino Crash Course 👇👇

We designed this circuit board for beginners!

SHOP OUR FAVORITE STUFF! (affiliate links)
---------------------------------------------------

We use Rev Captions for our subtitles

Arduino UNO R3:

Budget Arduino Kits:

Multimeter Options:

Helping Hands:

Soldering Stations:

AFFILIATES & REFERRALS
---------------------------------------------------

FOLLOW US ELSEWHERE
---------------------------------------------------

*Click Below to Read About This Topic on Our Website*

*Description:*
Arduino Course for Absolute Beginners

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 from 0 to 1023. What if we wanted to know the actual voltage being applied at the pin?

You Will Need

Potentiometer (any resistance range will work)
Jumper Wires - at least 3
solder-less breadboard
Persian Rug
Step-by-Step Instructions

Place your potentiometer into your breadboard.
Run a jumper wire from the 5-Volt pin of the Arduino either one of the outside pins of your potentiometer.
Run another jumper wire from one of the ground pins on your Arduino (labeled GND) to the other outside pin of your potentiometer.
Run the final jumper wire from pin A0 on your Arduino to the middle pin of the potentiometer.
Plug your Arduino into your computer.
Open up the Arduino IDE.
Go to File, Examples, 01.Basics, ReadAnalogVoltage
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.

This sketch does the exact same thing the last lesson covered 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 just to review what is taking place...

That is all there is to the setup of this sketch. the next block of code is loop(). Inside the curly braces of loop() the first thing we do is read the value at analog pin A0 and assign it to an integer variable called sensorValue.

int sensorValue = analogRead(A0);

Once we have recorded this value, we now want to convert it to an actual voltage. You will recall that the range returned by the analogRead() function is between 0 and 1023. We want this to reflect the actual voltage at the pin - which is between 0 and 5 volts depending on where we have our potentiometer turned to. So lets take a look at how we might accomplish this...

float voltage = sensorValue * (5.0 / 1023.0);

The first thing we encounter is a new data type - called float. A float is just a number with a decimal point; say for example 3.14 or 2.17781778. Floats, also called floating point numbers, can be huge in value, and take much more time for the Arduino to churn through than integers - this is why they are avoided unless necessary.

We want a float in this case because it will give us more resolution than an integer.

So what is that calculation anyway - it looks kind of confusing. The numbers on the right are just a conversion factor. We want to convert one scale to another.
Рекомендации по теме
Комментарии
Автор

These were made in 2013 and are still useful. Thank you so much. You really helped me learn Arduino.

mron-
Автор

These tutorials are absolutely amazing, great job!

joshualewis
Автор

Entire series well done...You're not just an instructor; but a teacher. Thanx...

lake
Автор

I wish I had found your tutorial earlier. But still I'm very glad that I found them. Thank you a million.

ginajung
Автор

Thank you for a brilliant series of tutorials. You are a great teacher. I can't praise this series enough. 
The tutorials are really clear and easy to understand for a complete novice like myself. I  feel like I have learnt something and will now be able to go on to develop my skills. 
Cheers! :) 

jonhughes
Автор

amazing....thank you so much....you make things so simple to watched all your videos from this playlist and i loved all of them....will always be thankful as you are the reason behind my basics of arduino ... thanks a ton!!!

dikshitadesai
Автор

Great video tutorials. Thank you for taking the time and for making this public and free! Re: Tutorial #9: I think it would help some less experienced folks such as myself if you explain why the float number changes with voltage. Like most, I experimented and found you don't need to change the code to get the 3.3v readings (well... more like 3.27), but I got stuck on the "why" this is the case until I read your response to a comment about 9 months ago explaining the float number is around 675 instead of 1023 with 3.3 volts. 

sflow
Автор

13:30 so what determines the number of the decimal points? in this case it's 2

Darkmatter
Автор

can you please tell me how can i write code to get voltage change per time for this analog read

arunboddu
Автор

Thank you! This is great. I'd been placing the Pot's middle wire on a digital
pin instead of an analog (A0) and getting weird results. Figured it out right
away after watching this tutorial.

steve-sioz
Автор

Is there a limit to how many lines are saved before the values converted are dumped out of memory? I'm trying to set up a way to record voltage for an undetermined amount of time, and have that data available in a csv or similar format. Alternatively in the worst case, i'd be able to copy and transfer those lines on the serial monitor if there is a very high "limit" for how many lines there are if that makes sense.

MinecraftsAssassin
Автор

Hello, how can i measure this output voltage in javascript ? It is possible from standardFirmata ? thanks.

domiinnno
Автор

I love these videos, very understandable, and so far, its making arduino very exciting and easy. Thanks for these videos!

pokemasters
Автор

Thanks for the tutorial; if the 5v is being divided by 1023 and the float can have 7 digits why does it return to two decimal places? can you you return more decimal place with different code?

cordelclimbs
Автор

Hi +Vishnu Das  - According to the Arduino Reference for Serial.print -> "Floats are printed as ASCII digits, *defaulting to two decimal places*. 

I didn't realize this until you asked and I checked!  Thanks for that.  Also, if you use the command:

 Serial.println(1.23456, 4); it should display "1.2346" on the serial monitor.  The second number passed is specifying how many digits to display.


Great comment - thanks for your thoughts!

programmingelectronics
Автор

Thanks a lot for your time and willing to share this video
I would like to ask you, why when I run this program and runs fine but I get the time of the day also with the readings, any ideas how to get rid of the time?Thanks for your advice..Victor

JUANITO
Автор

when i was coding C and C++, the float had 6 decimal points,
And you specified that in the videos as well.
But why does the serial monitor shows only 2 decimal points

lordofseas
Автор

I have a question. I have a trimpot and i dont know exactly what pin is what. It keeps giving me random values between 1.something and 3.something. Did I burn my GND?

sporeofdeath
Автор

Hello !
I need to create a "switch" between a 12V lead acid battery and a 5V dc power supply.
I want the system to switch to the dc power supply when the battery voltage drops under 11V. How can I do ?
thanks

antoinegpp
Автор

I have a problem just wondering if you could help ....) these can be adjusted up and down Variable2   Variable1 and I'm trying to get analog.pin A0 to recognize these two
so it will shut off or on pin6 can you help.;;;;

zx
welcome to shbcf.ru