analogWrite , which requires
an output value between 0 and 255, where 0 is off and 255 is full power. This is actually a
great way to control the brightness of an LED. If you were to try to control the brightness
by varying the voltage across the LED, you would find that nothing would happen until
you got to about 2V; then the LED would very quickly get quite bright. By controlling the
brightness using PWM and varying the average amount of time that the LED is on, you
achieve much more linear control of the brightness.
Analog Input Digital inputs just give you an on/off answer as to what is happening at a particular pin on
the Arduino board. Analog inputs, however, give you a value between 0 and 1023
depending on the voltage at the analog input pin.
The program reads the analog input using the
analogRead function. Sketch 6-09
displays the reading and actual voltage at the analog pin A0 in the Serial Monitor every
half second, so open the Serial Monitor and watch the readings appear.
When you run this sketch, you will notice that the readings change quite a bit. As with
the digital inputs, this is because the input is floating.
Take one end of the wire and put it into a GND socket so that A0 is connected to GND.
Your readings should now stay at 0. Move the end of the lead that was in GND and put it
into 5V and you should get a reading of around 1023, which is the maximum reading. So,
if you were to connect A0 to the 3.3V socket on the Arduino board, the Arduino volt
meter should tell you that you have about 3.3V.
Conclusion This concludes our chapter on the basics of getting signals into and out of the Arduino. In
the next chapter, we will look at some of the features provided in the standard Arduino
library.