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

Показать описание
🤩 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.
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.
Комментарии