Analog input circuit building is basically the same as digital input/output, we only have to connect the wire to analog input pin. While digital is used for only two conditions: true/false, we use analog when we want to get the variety of options. For these we can use a set of different resistors: the simplest one is potentiometer, then we have photo, force, stretch and few other.
In this lab we look into how some of these work.
The steps are as in the digital input/output lab:
- Set up the breadboard:
2. Add potentiometer and LED
Side legs of the potentiometer are connected to power and ground and the middle one acts as a voltage divider: we build it into one of the horizontal lines of the breadboard:
LED is connected same way as in digital lab: cathode to ground and resistor to power via resistor and jumper wire between them, which is then plugged in into digital pin (it will be coded as an output). The potentiometer’s middle led is plugged into the analog pin on the Arduino.
3. Program Arduino
What we do here is first set the variable to the output pin as ledPin, which is LED. In this case, if we later decide to change the pin, we don’t have to rewrite the pin number in every single line & only have to change it on the top of the code.
In the “draw/loop” function we code that we want to read the value potentiometer gives us when we move it.
When we turn the potentiometer to one side, we have LED shining very bright, which means there’s less resistance and LED being turned off when we turn potentiometer to the other side.
We can do the same with the photo resistor. The only difference is when we cover the photo resistor with hand, the LED becomes dimmer and brighter when we move the hand away.
UPDATE: I tried plugging in resistor once again, but accidentally, instead of putting LED’s resistor to power I put it to ground. This led to LED not getting dimmer but staying the same. Why so? In other words: why for the LED I’m putting the resistor to power and for the photocell to ground?
Try mapping the variable resistor’s range
I did it with the photo resistor. First, I struggled with the code as it gave me few times that the vars are not declared, then I somehow did it, it worked, but I still don’t get it. It would be great if we could go through it together.
While writing a sketch for both LEDs only one would turn on at the time: why would be so?
The question I have from analog input material is about decoupling capacitors: could we go through this together? I’m not sure I’m getting it.