Hardware 15
Hardware
For most inputs (potentiometers, thermsistors, photodiodes, phototransistors) you need to connect both the
input and a resistor. They are all connected in a similar conguration, which is called a voltage divider.
If you just had the sensor by itself, all the electricity would have to ow through it, regardless of how much
resistance it had. With another resistor, the electricity is split up, it is divided, between the analog pin and the
constant resistor. The less resistance your sensor has the more electricity will go through the resistor.
3.4: Using pinMode
We are now no longer using outputs, we are using inputs. This means we have to change how we use pin-
Mode. In the setup routine we have to choose what pin we are inputting into, the pins available are A0, A1,
A2, A3, A4 (Lesson 3, Figure 4). If we are using A0 it can be written as:
pinMode(A0,INPUT);
Or it can be written as:
pinMode(0,INPUT);
Writing the ‘A’ makes it more clear that it is the A0 pin, as opposed to the 0 pin which can only handle digital
information.
Lesson 3, Figure 4
Within the program it would look like this:
voidsetup(){
pinMode(A0,INPUT);
//usepinA0asananaloginput
}
voidloop(){
//dostu…
}
3.5: Using analogRead and Creating a Variable
If we want to use our input, we have to read from the pin. And we have to be able to use the information we
read out from our pin. The microcontroller reads the pin and gives you a number between 0 and 1023, which
represents between no voltage and full voltage.
The function to read from an analog pin is analogWrite, which may look like this:
analogRead(A0);