In this tutorial, we will go through the mechanism of how our street lights work. Pretty simple but very helpful in understanding how to get analog sensors working with Surilli. We will be using Light Dependent Resistors, simply known as LDRs, to detect the intensity of light in our surroundings. We can set the switching of LED to our own desired level of light intensity.
STEP 1: Plug In Your Surilli to the PCNOTE: If you are new to Surilli, follow our “Getting Started” and then return here. Link Getting started: http://surilli.silver-back.co/getting-started.html
Go to File>Examples>AnalogInput_LDR. The following sketch window will open.
STEP 3: Understanding the Code
Variables andTheir Function
- sensorPin: Select the input pin for the potentiometer
- ledPin: Select the pin for the LED
- sensorValue: Variable to store the value coming from the sensor
- Intensity: Light intensity at which you want to do LED switching (0-1023)
Check that you have selected the right board, processor and port number against your Surilli as follows.
After it’s all done, run the program from the right arrow button beneath the Edit tab, and wait until the compiler says, “Done uploading."
YOUR PROGRAM NOW HAS BEEN SUCCESSFULLY UPLOADED!
Try changing the lightning around the LDR to switch the LED ON/OFF.
Selecting Intensity Variable Value
“Intensity” variable in our sketch shows at which point or light intensity our LED will be switching on and off. You can change that value from 0-1023 according to your need. Hit and trial with different values to come up with your desired reference value of Intensity.
Complete Code
const int ledPin = 13;
const int ldrPin = A0;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(ldrPin, INPUT);
}
void loop() {
int ldrStatus = analogRead(ldrPin);
if (ldrStatus <=300) {
digitalWrite(ledPin, HIGH);
Serial.println("LDR is DARK, LED is ON");
}
else {
digitalWrite(ledPin, LOW);
Serial.println("---------------");
}
}
That’s all! Simple but a good one to start off with analog sensors. If you have any queries, you can always visit our website at surilli.io or contact support. Thank you!
Comments