Hello developers. 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 Resistor 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 PC.NOTE: If you are new to Surilli, follow our “Getting Started” and then return here.
Link for Getting started: http://surilli.silver-back.co/getting-started.html
Go to File>Examples>AnalogInput_LDR. The following sketch window will open.
Variables & Their 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. Use the Hit and trial method with different values to come up with your desired reference value of Intensity.
The project should be eventually working as follows:
int sensorPin =10 ; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0;// variable to store the value coming from the sensor
int Intensity = 200; // Light intensity at which you want to do LED switching (0-1023)
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
}
void loop()
{
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
//Serial.println(sensorValue);
if(sensorValue>Intensity)
{
digitalWrite(ledPin, LOW);
}
else if(sensorValue<Intensity)
{
digitalWrite(ledPin, HIGH);
}
}
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 surilli.io or contact our support. Thank you.
Comments