...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
#define BRIGHTNESSsensorPin A0 void setup() { Serial.begin(9600); } void loop() { int reading = analogRead(BRIGHTNESSsensorPin); // We get the sensor value here Serial.println(reading); // We send the message to serial } |
If we look at the values in the Serial Monitor, we see that they change as soon as we hold our hand over the sensor, for example.
Here is another example, where an LED is turned on when it gets dark.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
#define sensorPin A0 #define ledPin 13 void setup() { // put your setup code here, to run once: Serial.begin(9600); pinMode(ledPin, OUTPUT); } void loop() { // put your main code here, to run repeatedly: int sensorValue = analogRead(sensorPin); if (sensorValue <= 200) { // this value should be changed depending on your light conditions, resistors etc. digitalWrite(ledPin, HIGH); } else { digitalWrite(ledPin, LOW); } Serial.println(sensorValue); delay(50); } |
Potentiometers
A potentiometer is a special form of a variable resistor since it functions in itself as a voltage divider. This makes it perfect for generating a fast analog input. The function can be simplified as follows:
...