Skills Dev II: Working with Inputs and Sensors

Made by jmaffey · UNLISTED (SHOWN IN POOLS)

Prepare a simple 2-in (sensors or inputs) 1-out (LED) device with a simple use case for the device.

Created: November 10th, 2021

0

Outcome

This device is a burglar alarm. It uses a photoreceptor to detect if a door is open. If the door is opened, more light is let in, which trips the photoreceptor threshold and lights up the LED. The LED event is sent to the Particle Cloud where it triggers a text alert to your phone (this last part is represented by the LED lighting up). The device has a switch to turn the alarm on and off. When you leave the home, you turn it on; when you arrive back home, you turn it off. 


You can see it in action here: https://www.youtube.com/watch?v=QJxgrz5n1jo

0

Process

  • To make this alarm, there were two processes I needed to make work. First, the switch needed to turn on/off the alarm system (photoreceptor + LED), not the LED. Second, the alarm system threshold so that the LED would turn on only when the light sensor was > 3000. 
  • I struggled to figure out where how to adjust the void loop to make this work. If I separated each process one after the other in the loop, then when the switch was off, the light sensor would turn it back on as the loop repeated. So, I nested the photocell threshold instructions within the if/else switch statement. This created the desired effect. 

0

Reflection

  • The biggest challenge was learning the syntax of the if statements and getting the brackets in the right place. It was satisfying to start assembling different functions to solve this problem and build a small alarm system. 
  • If I were building this for real, I wouldn't use the LED; I would send the photocell data to the particle cloud and have the > 3000 reading trigger a text message to the user. For this project, I used the LED lighting up as a proxy for that notification. I did, however, include the code to send the photocell data to the particle cloud. 

0
// Define a pin that we'll place the photo cell on
// Remember to add a 10K Ohm pull-down resistor too.
int photoCellPin = A0;

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

// Define a pin we'll place an LED on
int ledPin = D2;

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

int switchPin = D3;


void setup()
{
  // Set up the LED for output
  pinMode(ledPin, OUTPUT);
  
  pinMode( switchPin , INPUT_PULLUP); // sets pin as input
  
    // Create a cloud variable of type integer
  // called 'light' mapped to photoCellReading
  Particle.variable("light", &photoCellReading, INT);
}

void loop()
{
   // find out if the button is pushed
   // or not by reading from it.
   int buttonState = digitalRead( switchPin );

  // Using a pulldown resistor we get a LOW
  // Signal when its on
  
  if( buttonState == LOW )
      {
        // turn the LED On
          // Use analogRead to read the photo cell reading
      // This gives us a value from 0 to 4095
      photoCellReading = analogRead(photoCellPin);
    
      // Map this value into the PWM range (0-255)
      // and store as the led brightness
     
      if( photoCellReading > 3000 )
        {
        // fade the LED to the desired brightness
        analogWrite(ledPin, 255);
    
    
      }else{
          analogWrite(ledPin, 0);
      }

  }else{
    // otherwise
    // turn the LED Off
    digitalWrite( ledPin, LOW);

  }
  
  delay(100);
}
Click to Expand
x
Share this Project

This project is only listed in this pool. Be considerate and think twice before sharing.


Courses

About

Prepare a simple 2-in (sensors or inputs) 1-out (LED) device with a simple use case for the device.