Projects Skills Dev 2: Working with Inputs and Sensors

Made by Xuan Peng ·

Use a pizeo element to detect vibration or a knock, and show it through the brightness of the LED.

Created: November 24th, 2023

0

Intention

The simple device is made for detecting my mom's footstep my room while I am with my earphone. By turning the device on, the flashing light can tell me whether my mom is approaching, so that I can take off my earphone and listen to her carefully without being yelled at. (Sorry mom, I know you are tender most ot the time)

0

Context

The device will detect vibration or knocks happen nearby, then transform it to the brightness of the led. The two varible of piezoReading and ledBrightness can also be read from the Particle Cloud, so that I can detect the condition of my room even if I am out, which might be very useful for pet owners with an enthusiatic dog.

0

Process

First I tried to simply turn the led on and off when the piezo detects vibration to a certain degree, but found the light keep flashing quickly. I thought this might be because I couldn't get an accurate handle on the voltage range of the piezo element, so I fixed its range using map() and brightened the initial brightness of the bulb to make it easier for me to see if it's on or not, rather than only relying on looking at the switch.

0

Product

I must say it was really difficult to place all these elements on a small breadboard:(

0
// Define a pin to place the Piezo Element
int piezoPin = A0;

// Create a variable to hold the light reading
int piezoReading = 0;

// Define a pin to place an LED
int ledPin = D6;

// Create a variable to store the LED brightness.
int ledBrightness = 0;

// Define a pin to place the switch
int switchPin = D4;

void setup() {

 // Set up the LED for output
  pinMode(ledPin, OUTPUT);
  
 // Set up the switch for input
  pinMode(switchPin , INPUT_PULLUP); 

  // Create two cloud variables for LED and piezo
  Particle.variable("Piezo", &piezoReading, INT);
  Particle.variable("LED", &ledBrightness, INT);
  
}

void loop() {
  // Use map() to adjust the brightness  
  piezoReading = analogRead(piezoPin);
  ledBrightness = map(piezoReading, 0, 20, 128, 255);
  int buttonState = digitalRead( switchPin );
  digitalWrite( ledPin, LOW );

  if ( buttonState == LOW ){
    
  analogWrite( ledPin, ledBrightness );
  }
  
  else{
    digitalWrite( ledPin, LOW );
  }
  
}
Click to Expand
0

Reflection and Next Steps

For Next Steps I might add the function to turn in on and off via Particle Cloud. Also I still need to improve my understanding of building physical circuits and try to always add a semicolon when coding.

x
Share this Project

This project is only accessible by signed in users. Be considerate and think twice before sharing.


Courses

48-675 Designing for the Internet of Things

· 11 members

A hands-on introductory course exploring the Internet of Things and connected product experiences.


About

Use a pizeo element to detect vibration or a knock, and show it through the brightness of the LED.