You can think of the conversion calculation by saying “sensorValue is to X volts as 5 volts is to 1023”, where X is the converted voltage value we are trying to determine. By multiplying sensorValue by (5.0/1023.0), it scales down the range from 0-1023 (which is the range that analogRead() returns) to the range 0-5 which is the actual range of the voltage. We use a conversion factor to accomplish this feat. Our goal is to take the value that analogRead() returns and convert it into an actual voltage value. The voltage variable is set equal to a somewhat confusing calculation.
We want the voltage variable set as a float data type because it will provide more resolution than an integer. 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 used only when necessary. A float is simply a number with a decimal point say for example 3.14 or 2.17781778. The first thing we encounter is a new data type – called float. So let’s take a look at how we might accomplish this… We want this to reflect the actual voltage at the pin – which is between 0 and 5 volts depending on where the potentiometer knob is turned. You will recall that the range returned by the analogRead() function is between. Once we have recorded this value, we want to convert it to an actual voltage. 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.
That is all there is to the setup() of this sketch. Inside the curly braces of setup() we begin serial communications by setting the baud rate. We have no variables to declare and initialize at the beginning of the sketch so we jump right into the setup() function. Let’s start from the top to review what is taking place. It takes the reading provided by the analogRead() function and converts it into the actual voltage value at the respective analog pin. This sketch does the exact same thing as the last lesson sketch except for one important change.
Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):įloat voltage = sensorValue * (5.0 / 1023.0) the loop routine runs over and over again forever: initialize serial communication at 9600 bits per second: the setup routine runs once when you press reset:
#ARDUINO SERIAL PLOTTER SCALE CODE#
This example code is in the public domain. Graphical representation is available using serial plotter (Tools > Serial Plotter menu)Īttach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground. Reads an analog input on pin 0, converts it to voltage, and prints the result to the serial monitor. Now adjust the knob of your potentiometer and watch the serial monitor window, the numbers should adjust between 0 and 5.On the menu bar, go to Tools > Serial Monitor – this will open the Serial Monitor window – you should see numbers rolling down this screen.It will turn orange and then back to blue once it has finished. Click the Upload button (next to the Verify button).Click the Verify button on the top left side of the screen.Run the final jumper wire from pin A0 on the Arduino to the middle pin 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 a jumper wire from the 5-Volt pin of the Arduino to either one of the outside pins of the potentiometer.Place the potentiometer into your breadboard.Potentiometer (any resistance range will work).By submitting this form you agree to the privacy policy, and can opt-out anytime. You will receive email correspondence about Arduino programming, electronics, and special offers.