In this tutorial, we will be using ball tilt switch sensor. We are going to program our Surilli with a basic program so whenever there is a change detected you can perform different tasks from its ouput.
Ball Switch Sensor (KY-020)This sensor contains a small metal ball which will complete a circuit depending on the position in the sensor. Because the sensor is very basic, it can only detect large changes when its tilt, and can not measure the angle of its tilt. It can be used to detect changes, like for example, opening or closing of door, etc.
STEP 1: Set Up Arduino IDE for SurilliMake sure you have selected the right port, board and processor for the Surilli and it is programmable (compile and upload “Blink” from File>Examples>Digital>Blink onto your Surilli to check if everything is working fine).
STEP 2: The CircuitryThe circuitry is very simple. It's mostly the programming, follow the figure below to set up your hardware.
- Now you have completed setting up your hardware and Arduino IDE. Copy and paste the Arduino sketch given below into your Arduino IDE and hit upload.
- After it is uploaded, the sensor will start working.
int Led = 5;// define LED Interface
int buttonpin = 4; // define Sensor Interface
int val = 0;// define numeric variables val
void setup ()
{
pinMode (Led, OUTPUT) ;// define LED as output interface
pinMode (buttonpin, INPUT) ;// output interface is defined of sensor
}
void loop ()
{
val = digitalRead(buttonpin);// digital interface will be assigned a value of pin 4 to read val
if (val == HIGH) // When the change is detected module detects a signal, LED flashes
{
digitalWrite (Led, HIGH);
}
else
{
digitalWrite (Led, LOW);
}
}
Play with the program to see how it reacts to different values and logic. This will develop your understanding about ball switch sensors so you can use them in your practical application.
If you make something fun and interesting, do share it with our community!
That’s all for now. If you have any queries, visit surilli.io or contact our support. Stay connected with Surilli family for more amazing stuff. :-)
Comments
Please log in or sign up to comment.