Arduino: Advancing to Analog
An Introduction to the Arduino Platform
Part 1 | Part 2 | Part 3 | Part 4

In my last post I introduced the Arduino platform and demonstrated a very simple project using the digital pins. This time I’ll delve a little deeper into some specific details and demonstrate a slightly more advanced project using the analog input pins.
Breakdown of the Board
The Arduino board has three main sections of pins. The digital pins (at the top of the diagram below) can each be set as either input or output pins. As an input pin, they can tell you whether they’re receiving power or not (although not how much). As an output pin, they can only output full voltage (5 volts) or none at all.

In contrast, the analog input pins function the same way as the digital input pins, except they give you a value between 0 and 1023 that corresponds to the power level that they receive. These can be useful to receive input from components such as knobs or, as we’ll be using later, photoresistors (a kind of light sensor).
Beside the analog pins are the power pins. The only pins here that we care about at this point are the 5V and GND pins. If you’d like to understand this in more depth, I’d suggest looking up the basics of electronic circuits, but for now just know that every electronic component has at least one connection going to a power source (the “positive” end) and another going to “ground” (the “negative” end). The 5V pin outputs full voltage just like the digital output pins do, and would be connected to the positive end of a component. The GND pin acts as the ground, and would be connected to the negative end of a component. Notice that there is also a ground pins next to the digital pins.
Pulse Width Modulation

One of the limiting factors of the digital pins is that they can only output an “on” or “off” value. This means if you want to, for example, have an LED repeatedly fade in and out, it would seem impossible. However, if you can switch the LED on and off rapidly enough that the human eye can’t perceive the blinking, you can essentially fake different brightness values. A 50% brightness value can be achieved by switching the light on and off at an equal rate, or a 25% brightness value can be achieved by having the light switched on only 25% of the time. This is the concept of pulse width modulation (PWM), and is available to every digital pin that has a “~” symbol next to it. I’ll demonstrate this in the example below.
Example: Ambient Light-Sensitive LED
In this example, we’ll connect an LED and a photoresistor to the Arduino board. A photoresistor responds to the amount of light it receives by blocking the current of electricity running through it. The less light it receives, the less electricity it allows through, and vice versa. Our aim will be to have the LED shine brighter in brighter surroundings, and darker in darker surroundings. The LED will be connected to digital pin 13, and the photoresistor will be connected to analog input pin 0.
To start off, let’s declare our variables:

The first two lines allow us to later refer to the digital pin 13 as ledPin, and analog pin A0 as sensorPin, just for clarity. Next, sensorValue will store the reading of the photoresistor, and ledVal will store the value to be sent to the LED pin.
Next, our setup code is as simple as it gets:

This simply sets the LED pin to be in “output” mode. And finally, the interesting part:

The first line in the function uses the analogRead function to read the value from the pin the photoresistor is connected to, and stores it in sensorVal. We can’t output that value to the LED pin, because analogRead stores values from 0 to 1023, and analogWrite (the function used to send values to the PWM digital pins) sends values from 0 to 255. So, the map function on the second line converts the larger range to the smaller range. The first argument specifies the initial value to convert, the second and third arguments specify the lower and upper bounds of the input value, and the fourth and fifth arguments specify the lower and upper bounds of the output value. So, a reading of 1023 should return 255, a halfway reading of 512 should return 128, and so on.
Next, the analogWrite function takes the new value stored in ledValue and sends it to the LED pin, causing it to shine brightly or darkly depending on the environment. And lastly, the last line simply limits the frequency of updates to once every 10 milliseconds.
Alternative Mappings
Rather than using the map function, we could have simply divided the sensor value by four to get the desired value. However, the map function allows for other scenarios — you can switch the last two arguments around the get the LED to respond in the opposite way: brighter for darker rooms, and darker for brighter rooms. Or, perhaps the sensor will never experience a pitch-black room, in which case you could change the range from 0–1023 to something like 100–1023. Overall, this function allows for a lot of flexibility.
Armed with only the few functions covered in this and my last post, there are already a surprising amount of interesting contraptions that can be built. And while I’ve only focused on the Arduino UNO board, there are a multitude of different Arduino boards with different pin layouts, faster processors or smaller form factors. Check out the Arduino site to learn more!