Это видео недоступно.
Сожалеем об этом.
Tutorial 09: How to read voltages with analogRead(): Arduino Course for Absolute Beginners (ReM)
HTML-код
- Опубликовано: 18 авг 2024
- 🤩 FREE Arduino Crash Course 👇👇
bit.ly/get_Ard...
**If you like this, I think you'll like the premium Arduino training we offer. Check it out here** bit.ly/3nSBPUs
We designed this circuit board for beginners!
Kit-On-A-Shield: amzn.to/3lfWClU
SHOP OUR FAVORITE STUFF! (affiliate links)
---------------------------------------------------
We use Rev Captions for our subtitles
bit.ly/39trLeB
Arduino UNO R3:
Amazon: amzn.to/37eP4ra
Newegg: bit.ly/3fahas8
Budget Arduino Kits:
Amazon:amzn.to/3C0VqsH
Newegg:bit.ly/3j4tISX
Multimeter Options:
Amazon: amzn.to/3rRo3E0
Newegg: bit.ly/3rJoekA
Helping Hands:
Amazon: amzn.to/3C8IYXZ
Newegg: bit.ly/3fb03X1
Soldering Stations:
Amazon: amzn.to/2VawmP4
Newegg: bit.ly/3BZ6oio
AFFILIATES & REFERRALS
---------------------------------------------------
►Audible Plus Free trial: amzn.to/3j5IGrV
►Join Honey- Save Money bit.ly/3xmj7rH
►Download Glasswire for Free:bit.ly/3iv1fql
FOLLOW US ELSEWHERE
---------------------------------------------------
Facebook: / programmingelectronics...
Twitter: / progelecacademy
Website: www.programmin...
Click Below to Read About This Topic on Our Website
www.programmin...
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...
We have no variable 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).
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.
****If you like this, I think you'll like the premium Arduino training we offer. You can check it out here**** bit.ly/3lHyzcB
Surely there are 1024 values in 0-1023 ?
These were made in 2013 and are still useful. Thank you so much. You really helped me learn Arduino.
Glad you liked them!
still helping me in 2022 very good videos
These tutorials are absolutely amazing, great job!
Thanks for watching! You should check out the new ones too.
Programming Electronics Academy ARduino courses
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.
sflow15 Thanks for your comments and thanks for pointing that out!
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! :)
Jon Hughes Fantastic - so glad to hear they are helpful!
I wish I had found your tutorial earlier. But still I'm very glad that I found them. Thank you a million.
amazing....thank you so much....you make things so simple to understand.....i 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!!!
+dikshita desai Thank you very kindly - I really appreciate that!
Entire series well done...You're not just an instructor; but a teacher. Thanx...
Thanks for your kind words - I hope the lessons helped!
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.
Glad it helped!
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.
Here is a link to the Serial.print reference arduino.cc/en/Serial/Print
Great comment - thanks for your thoughts!
Hi. can serial print or software SPI (for OLED with U8glib) intefere with Analog reading. I have this Pro Mini which is essentially Atmel Mega328P, whose analogRead() for all A0 to A7 produces 1023 output (digital i/o is still functional on these pins). I've tried setting pinMode(A*,INPUT) and then digitalWrite(A*,LOW) just incase the pullups were activated. I'm thoroughly confused
I love these videos, very understandable, and so far, its making arduino very exciting and easy. Thanks for these videos!
Glad they have been helpful!
I like your style of teaching 👍👏
1.yes we have to change 5 volt to 3.3 volt to get the accurate value.
2: we have to change the value of volt in conversion formula.
Just a quick tip here: (5.0 / 1023.0) always stays the same, so you don't need to do this computation over and over again in loop() method. You can simply do it once in the setup() method and store the result in a variable and then use it in the loop().
awesome teaching and learning .. loved it so much
I might be very likely off here, but isn't it supposed to be divided by 1024? 0 being first step and 1023 being 1024th step? It won't affect the result by significant bit though :)
Miroslav - you are not crazy at all - thank you for pointing that out!
Awesome! Who needs to buy a voltage reader when you can build one!
Thanks for watching! I hope it was helpful.
Outstanding videos, thank you so much.
Glad you like them - thank you for the note!
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
@akashic seer the trick to print more decimals: Serial.print(voltage, 2)
You van change number behind to the decimals you want
Pretty old comment btw
@@robv.7644 So by 2 we will get a float value upto 2 decimal digits??
@@kodexe3298 yes
I am a big fan of your work! Say, how would you read a voltage from a sensor on one pin, then output a different, but corresponding voltage to a different pin?
Thanks Thurl and great question! You could use a voltage divider circuit where you the read the incoming voltage with an analog pin (do a simple conversion in your code), and then use PMW on a digital pin to output.
If you look at the Arduino IDE > Examples > Basics > ReadAnalogVoltage
And also check out the PWM examples -that should help - best of luck!
Amazing video bro, helped me a lot. Keep up the awesome vids.
For range output in between 0-100 volts...You have to adjust the formula as:
float voltage= sensorValue*(100.0/1023.0);
For Aurduino 3.3V pin....The formula changes as:
float voltage= sensorValue*(3.3/1023.0);
The pin input will not take more than 5V so DON'T try to connect 100 Volts in to it! Use a voltage divider to bring the 100V down to 5V!
Correct me if I'm wrong, please, but I think that the code should read: float voltage = sensorValue * (5/1024.0) ! There are in fact 1024 units, since 2^10=1024. It does not matter that the count begins at zero and not at one! (0-1023)>>1024 units. (1-1024)>> 1024 units. Granted: the difference is tiny, but I think the statement in the code example: float voltage = sensorValue * (5/1023.0) is inaccurate!
I tell you what, you make this so simple. Thanks!
Glad to here this - thanks for the feedback!
0-1023 is 1024 values. The formula should be "float voltage = sensorValue * (5.0 / 1024);"
clear & crisp expalnation. tyu
Thanks a ton for checking it out and your kind words.
changed the float and used unsigned long and did not get any decimal numbers but just the whole numbers. so if using float gets you to 2 decimal isn't better but slows down things than using unsigned long which gives you whole numbers well rounded off i guess
5 volts divided 1023 times is 0.0048875855, which is the voltage per 1 of 1023 divisions, or resolution. Double data type ftw.
The extra challenges are good because I usually mess something up and have to problem solve which is where the real learning happens. I forgot to put the pin back to 5V from 3.3V when I went to change the range to 100... LOL
David D So true! It seems that problems (and solving them - or even just pondering them) are at the heart of learning.
13:30 so what determines the number of the decimal points? in this case it's 2
Excellent video. But how if i want to use all 6 analog input channels and read them individually in serial monitor, ? I cannot extrapolate the code for 6 channels. Pls help. Thank you.
Of course you can, you can add interrupts or call function to show each value by using the reset button on the arduino.
Wonderful tutorial. Thanks so much!
yuval gerstein Thanks for watching and the kind note - much appreciated!
This is worth subscribing to
Could you explain why the program doesn't output the voltage on the serial monitor if you use 5 and 1023 instead of 5.0 and 1023.0 ?
Because float doesn't display real numbers, you need a decimal value.
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?
Amazing!,this help me a lot!
You can get the same results by just multiplying by 0.0048875855327(which is the product of 5/1023) in the Serial.println command, in the same code from the last tutorial. Like this
void setup()
{
//Initialize serial com at 9600 bit/s baud rate
Serial.begin(9600);
}
void loop()
{
//Declare variable for the value of analog pin (A0)
int voltageValue = analogRead(A0);
//Print voltageValue (A0) to serial monitor
Serial.println(voltageValue * 0.0048875855327);
//1 millisecond delay for readability
delay(1);
}
Answer no.1 nope. no code changing needed
answer no.2 change {sensorValue * (3.3 / 1023.0)} to {sensorValue * (100 / 1023.0)}
What's my mark? 0,50, or 100?
To get the serial monitor to display on a scale from 0-100 using the 3.3V source. I changed the 1023 to 675.18
How did I come to that number? 1023/5 = 204.6 now I just multiply that by the max voltage 204.6 X 3.3 = 675.18
Anyway your question is a year old, so you probably know all this stuff and a lot more by now,
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
How to read voltages from sensors sir? I really need it for my research. Thank you so much
I had some trouble on this one and the one before, as the elegoo starter set had some potentiometers that were pronged like mewtwo's hands, so if anybody else has those issues, just alligator clip them.
Loving these ! good work !
These tutorials are superb
hey Mike, when do I use the juice cup?!
It's for a little Mimosa if everything goes as planned.
Use the basic conversion formula he showed in the video, 3.3 / x units = 5 / 1023 units.
can you please tell me how can i write code to get voltage change per time for this analog read
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.
I am guessing that this method will not be a viable option for reading a thermocouple then? since it would need to read
Amazing tutorials!keep it up!
+Ana Dino Thanks for that!
very helpful.... do u have tutorial on micro controller?
Hi. Nice video. How to measure voltage of another source? Like an voltmeter (e.g. internal 3v,measures external 50v).hint would be great. Greetings
another thing when we do the 5.0 / 1023.0 can we do 12v so 12.0? as i would be using DC small motors 12v capable who i will power the arduino thru the VIN which i think is for max voltage 12v ? and do the math
SensorValue * (12.0 / 1023.0) = map(Sensor Value, 0, 255);
so now we can match/map the lowest volt to the min motor to the max motor voltage to max pot range?
I have discovered some relation between the sensor value and the input voltage, but I just wonder why (204.6) must be the basic number of this relation? is there someone to give us some information about it?
as I pointed, if we use (3.3V), we need to multiply 3.3 by 204.6 which is equal to 675.18.
i get 1023 when nothing is plugged in aswell
or just all the time...
great video ! extremely helpful
but i need to know can i measure power consumed by a load using arduino?????
Is it also possible when a power supply will be connected to the arduino. Will it know the applied voltage when you turn the potentiometer?
Took me a little while but I got the thing to read from one to one hundred by changing the 1023 to 51.15.
But, you just needed to change the 5.0 to 100.0
I have a question. Is it really necessary to hook up the voltage to be measured to the Analog Pin 0, and why? Can I just any of the other 2, 3, 4 or 5 pins? Or is PIN 0 the only ADC pin on an arduino?
No delay needed because of the float slowing it down?
Why isn’t there a delay(1); in the loop for stability? Do you only need the little delay if you use a digitalWritw()?
would someone please post a video about programming for midi electronic drums?! everyone uses the LED sketch as a demostration.
Hello, how can i measure this output voltage in javascript ? It is possible from standardFirmata ? thanks.
Hi, could you please tell me why you use 1024 or (2^(10) value? how can I find the maximum value of analog input if my controller voltage range is 0 to 10V?
Did this searching a while ago and wish I stumbled on this message weeks ago... By default the float value will limit to 2 decimal places unless told otherwise with the ,x number of decimal places.
my arduino shuts off if i bring the knob to the far left, why is that ? im not using a resistor in place just small servo 5g reused pot it does this in 3.3v and 5v as well and the LED gets brighter the higher i go 1023 but back down or far left and the LED dims till it shut off im using a nano newest ver or arduino ide
What will happen if i apply negative voltage on analoge pin i mean if i changed the polarity of dc voltgae which are given on analog input pin?
why is there no delay for stability, and why did the float not have total 7 digits with 6-digit precision to the right of the decimal?
I dont get it, I coded it a lil differently tho still the same end, where instead of using int and float i just had it for void loop:: analogRead (AO), and then Serial.println(A0*(5/1023). All i get is 0.7 over and over. Even when changing small things such as deleting int sensorValue and making a float integer for voltage. I was just playing around to see how much I could shorten the code while still functional.
why no delay for stability? would you have longer delays for complex math?
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?
Why is there no delay method in this code as in the previous video
Great tutorial. From Slave Unknown With Love!
ok i did the challenges when i swapped the voltage to 3.3 it worked as advertised when i went from 0-100 worked as advertised but for fun i tried to change the code to read 3.3 volts in the equation and it only read a maximum of 2.18. why?
Whats your code look like?
Open Source Hardware Group the code I used
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 - 3.3V):
float voltage = sensorValue * (3.3 / 1023.0);
// print out the value you read:
Serial.println(voltage);
}
and I changed the wire to the 3.3v output on the board
Trying changing
float voltage = sensorValue * (3.3 / 1023.0)
to something like...
float voltage = sensorValue * (3.3 / 675)
The most that analogRead is going to return when only connected to 3.3V is something like 675ish.
Thank you very much sir
Great Tutorial, love it!!
is it possible to read voltage by just using a positive and negative terminal? i have a PH probe and only two wires (anode and cathode) come out of it.
in this case what would be my wire configuration?
Thanks in advance!
I know this was a while ago now but I guess you would attach the cathode to ground and the anode to an analog(ue) input such as A0. What is needed is a potential difference applied across ground and an analogue input.
why do i not get full range on my potentiometer. i get 0 and 5 v way before i have gone the whole distance of the pot? im very new to all this
The voltage only reads one value before the decimal point so it only shows 0.00 when it should show 100. How can I change that?
Sorry but I've got a problem, I've done the same thing but I've got an oscillating signal in the serial monitor varying from 0 to 5 and back, I've used a 50k pot.
Great videos..
Glad you like them!
i did the try on you own challenge but in the first challenge the maximum value output for the voltage in the serial monitor was 2.3 and not 3.3, aby idea why? please help.
Thanks for the tutorial. I got it going , no prob, and changed the voltage to read 0 to 100.
However , I need to read 2 pots. Checking on arduino forum didn't give a straight answer. I've no idea what I need to declare etc to add another analogue port eg analogue(1).
+binarybox After several attempts, I have used voltage1 and pot1 and voltage2 and pot2 and got it working by copy and pasting the code in the void loop. .... 8-)
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
would you be willing to help me with some code ? code works perfectly on a arduino pro micro however i now want the code on a arduino uno. the temperature readings are incorrect and its due to the voltage reference not working.
on the pro micro pin D9 Connected to VCC (5v) and this made temperature readings correct.
on the arduino uno i have AREF and OIEF and D9 connected to vcc (5V)
But will not work. really hope you can help as i cant figure it out im sure its a code problem im just not very experienced
What if I wanted to have the word "volts" attached to the output as read in the serial screen? What would be the code. thx
hey Michael, is it possible to measure voltages above 5 volts. I need to make a battery charger and would like to use an arduino to set a voltage reference between 10 to12 volts, any ideas thanks mike
snaprollinpitts you will need a resistor divider network to accomplish that. You can also use the potentiometer; however, because the pot is variable, it is possible to accidentally present a voltage exceeding 5 Volts to the analog input. A fixed resistor-divider network will guard against that.
why there is no delay function included here ?
in my code when i tried the calculation as "float voltage = sensorValue*(5/1023);" (and not 5.0 or 1023.0) i get serial prints of only 0s no matter what the position on the potentiometer. If a variable is float, its input numbers must have a decimal point in it? whats happening here?
if I press a button on a LCD shield, how do I get the select button to run the loop after I press it, I am trying to make a digital dice to play catan with. heres what I have so far #include
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int button;
int btnselect_button;
int randNumber;
int lastButtonState;
void setup() {
lcd.begin(16, 2);
lcd.setCursor (5, 0);
lcd.println ("catan" );
button = analogRead(A0);
}
void loop() {
randNumber = random(1, 7);
Serial.println(randNumber);
if(analogRead(btnselect_button) == 0 && lastButtonState == 1) //if button is pressed and was released last change
if(analogRead(btnselect_button) == 1 && lastButtonState == 0) //if button is not pressed, and was pressed last change
lastButtonState = 1; //record the lastButtonState
}
but please sir, how about negative voltage ?? how can we get a alternating voltage via analog read
Great series !!:))
How come you have to initialize output / input pins for the digital pins (in the Void Setup) but you don't have to do that for the analog pins?
Also, am I correct in saying you never have to initialize the ground and voltage (5V and 3.3 V) pins?
Thanks!
Vivek Gupta More great questions.
By default, the Arduino will *automatically* set the modes of all pins to INPUTS unless you specify otherwise.
On a side note: You will see me specifying pins as INPUTS, I do this because to me it is more *explicit* to me when reading the program - it helps remind me that a pin has been set to an input.
( Also, If you are not using pins in a program/project, then you don't have to worry about initializing them. )
You are correct, you do not need to initialize the ground and/or 5V/3.3V pins.
Have a good one!
If I put the analog signal line in the other analog pins (A1 to A5) I get analog reading of 269 which means there is approx 1.3V on those pins. Should that be the case or is something wrong with my Arduino?
Hi Tommy Productions , just out of curiosity) did you change the code in the Arduino sketch to sample at the other pins? Ie., did you change analogRead() to look at pin A1 for example? If not, then the Arduino is still looking at Pin A0, even if you don;t have the signal attached.
Thanks for the reply. I did change the sketch to read A1 and moved the connection from the middle pot pin to A1. As expected it read 0-5V. Out of curiosity I checked the other pins that were not connected to the middle pin. I expected to read 0V but consistently read a voltage on the non connected (non declared) pins. Not critical .. just hunting and pecking as you suggested. Great (very understandable) tutorials! Thanks
Hi Open Source, I'm learning with your tutorials, they are awesome! Now I tried the homework changing the pin and code from 5.0V to 3.3V, and the highest number the potentiometer gives me in the serial monitor is 2.29... could that happen because I am using INFIDUINO (chinese clone) instead of Arduino...? or could it be another reason?
Hi Jorge, that is great question. In the first challenge, you don't need to change any code - just voltage from pin from 5 to 3.3, and you the output at the serial monitor should change to about 3.3V
Thanks!
Open Source Hardware Group Hi! Thanks so much for your videos. I really learned a lot. I thought you needed to modify the code to 3.3 / 1023. Can you please explain why this is not necessary?
@@racerx1326 It's because 5V is logic high even if you are using the 3.3V supply pin. So the conversion factor doesn't change. I know your comment is old but it helps anyone else reading along
@@claytonbinns7679 thanks i was wondering that, and thanks for answer
why u not use the delay in the program
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.;;;;
I believe I am missing something...
So, we use and ADC pin to read an analog value (between 0 and 5V). Then, we use analogRead which will give us a discrete value... after that we convert to analog again with a mathematical operation.... Why didn't we read the original value in the first place? Is this always necessary? Thanks!!!
Oh, and congratulations for these tutorials!!
Each of the analog inputs has a 10 bit analog to digital converter (ADC). What does that mean?
It means that the input will take a refence value (5V unless you change it in the sketch) and compare to your imput value (the voltage on the middle of the potentiometer). Then it will take that value and turn into a 10 bit number and send to your monitor (10 bits = 2^10 = 1024 possible values). So we have to turn this number back into a voltage value to print it on the screen.
it's a nice move to tell that the setup was made using fritzing. Since they only get money from donations.
i cannot compile this project error comes while compiling .
i have not connected my arduino and potentiometer so this problem occurs? or some other?
the code written are correct then also i cannot copile please help
Why no wait function? Is the delay not necessary?
Ok I did the challenge at the end changing from 5.0v to 3.3v no problem. I changed the divider from 5.0 volts to 3.3 volts everything worked fine moved my connector to 3.3 power hole code float voltage = sensorValue * (3.3 / 1023.0);. Then i used the 100.0 divider for 5.0 volts of current and i got value up to 100.00. I then set my current to 3.3 volts and the max i could get for my float voltage was 37.25 is that right answer. float voltage = sensorValue * (100.0 / 1023.0);