1 minute read
How to Measure Voltage with an Arduino
CHAPTER 6: Hacking Arduino 119
Notice that the “setup” function now has two new commands in it.
Serial.begin(9600); Serial.println("1=On, 0=Off");
The first of these starts serial communications over the serial port at 9600 baud. The second sends the prompt message, so that we know what to do when the Serial Monitor opens.
The “loop” function first uses the function “Serial. available()” to check if there is any communication from the computer waiting to be processed. If there is, then this is read into a character variable.
We then have two if statements. The first checks to see if the character is a “1”, and if it is, it turns on the toy. If, on the other hand, the character read is a “0”, it turns it off.
We have made a little bit of a leap from our first flashing sketch, and if you need more help understanding how the sketch works, you might consider buying the book Programming Arduino: Getting Started with Sketches by this author.
How to Measure Voltage with an Arduino
The pins labeled A0 to A5 on an Arduino are analog inputs. That is, you can use them to measure voltage. To demonstrate this, you will use the variable resistor (trimpot) as a voltage divider connected to A3 (Figure 6-16).
Figure 6-16 A variable resistor and Arduino