Screen Distance Switch | Creative Switches – Week 9

Concept

For this week, we had to make a creative switch. I wanted to use one of the sensors that came along with the Arduino kit to build something simple that could be useful if improved upon later on. I noticed the ultrasonic sensor HC-SR04 that came along with the kit and checked out its documentation to see how it works . Although we weren’t required to use code for this, I realized that all I needed to do to create a switch with this sensor was to install a library and connect it to an input pin . 

Thinking about applications , I recalled reading about the screen distance feature on some mobile phones where users who use their phones at less distance from their face for extended periods of time get a warning .

I set 0-50 cm as the range where the LED light ups to indicate that you are too close to the screen and placed the breadboard and the sensor on my table just above where I usually use my laptop. 

Images 

Video Demonstration

Video link – https://youtu.be/iuf76Mcibu8 

Code

The Arduino code is below : 

#include <NewPing.h> //include the header file 
// Aadil Chasmawala - 31st March 2024
const int LEDPin = 11; // Define LEDPin as a constant integer
NewPing Mysensor(8, 10, 200); // trigger, echo, max_distance = 200cm

void setup() {
  pinMode(LEDPin, OUTPUT); //set LED pin mode to output 
}

void loop() {
  int distance = Mysensor.ping_cm(); //get the distance 
  if(distance > 0 && distance < 50){ //if distance is too close 
    digitalWrite(LEDPin, HIGH); //light up LED
  }
  else{
    digitalWrite(LEDPin, LOW); //switch off LED
  }
  delay(100); //to ensure that the LED doesnt blink too quickly
}

I used a library called NewPing to easily create a NewPing object that can take input from the ultrasonic sensor HC-SR04 . The trigger pin is 8 ,ECHO pin is 10 , max_distance of operation is 200 cm and the LEDPin (for output to the LED ) is 11 . 

This code along with the connections creates the effect of a switch that lights up when one moves close to it , 

Challenges and Reflections 

One of my resistors in the circuit wasn’t working and it took me quite some time to figure out what was wrong  . 

I had initially thought of using a specific sound as a trigger (like a specific song or something simpler like 2 claps  that lights up the LED )  but realized that I would need a sound sensor for this. Hopefully, I will try building something with a sound sensor for one of the assignments as it is very interesting to me . 

In addition , the ultrasonic sensor is very useful too ,  however it operates only in one direction and to accurately get a sense of direction , you would have to use multiple ultrasonic sensors –  something that ?I could explore in future projects . 

 

Leave a Reply