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 Basic M0. 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 and 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. Hit and trial with different values to come up with your desired reference value of Intensity.
Complete Code:
int sensorPin =A0 ; // 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 at surilli.io or contact support. Thank you!



_A8Tr3ZJb1N.png?auto=compress%2Cformat&w=900&h=675&fit=min)










Comments