IDMT
Arduino: Analogue input and output
Prof. Nick Bryan-Kinns
Learning Objectives
Read analogue values from sensors
Understand different kinds of sensors
Write analogue values to Arduino output pins
Arduino
Digital inputs/ outputs
USB
Power & Analog inputs
ground
Digital, Analogue
Digital: ON or OFF
e.g. button
Analogue: range of values
e.g. a dimmer switch
Connect a Potentiometer
a.k.a. a “pot” or knob
The pot has 3 pins
5V and GND on the outside
Arduino A0 in the middle
pin A0
5V
GND (ground)
Circuit diagram
Connect a Potentiometer
a.k.a. a “pot” or knob
The pot has 3 pins
5V and GND on the outside
Arduino A0 in the middle
pin A0
5V
GND (ground)
Using analog sensors
Open AnalogReadSerial sketch
Reading values
Serial monitor
(after you’ve uploaded
your sketch!)
Readingand
Sensors values
LEDs
Serial monitor
(after you’ve uploaded
your sketch!)
Open AnalogInput sketch
What happens to the LED when you turn the knob?
Connect a phototransistor
Back to this sketch...
Open AnalogReadSerial sketch
What is the range of possible values?
map() and constrain()
In here add:
int newValue = map(sensorValue, ??, ??, 0, 1023);
the minimum the maximum
In here add:
int newValue = map(sensorValue,
And change:
[...], [...], 0, 1023);
Serial.println(newValue);
and change:
Serial.println(newValue);
map() and constrain()
You could also constrain the value:
int newValue = map(sensorValue, 300, 700, 0, 1023);
newValue = constrain(newValue, 0, 1023);
In here add:
Serial.println(newValue);
int newValue = map(sensorValue,
[...], [...], 0, 1023);
and change:
Serial.println(newValue);
Analogue Output
Connect an LED** Light-Emitting Diode
Long lead goes
to the resistor
pin 9 560Ω
resistor
GND (ground)
Fading sketch
analogWrite()
What is the range of values?
for(start; finish; increment) {
// stuff in the loop
}
Learning Objectives
Read analogue values from sensors
Understand different kinds of sensors
Write analogue values to Arduino ouput pins