Shivani Kannan - Working with Inputs and Sensors

Made by Shivani Kannan ·

Integrate sensing into the circuits, read information from the environment around, and translate that into an LED output.

Created: November 7th, 2024

0

Intention

Integrate sensing into the circuits and read information from the environment around. When the switch is turned on, the LED output changes based on the input.

0

Process

Components: Breadboard, Light Sensor, Switch, LED, Jumper Wires, 2 Resistors

1. Building a circuit with 1 input and 1 output: light sensor and LED

2. Writing code for the LED brightness to change based on the lighting

3. Debugging: the light in the surroundings was too bright, so I had to modify the code adjusting the conversion scale based on the brightness.

4. Modifying circuit to include a second input: switch

5. Modifying code so the computer first checks if the switch is on and then gets data from the light sensor to modify the light brightness on the LED.

0
#include "Particle.h"

int photoPin = A0;
int photoReading = 0;
int ledPin = D1;
int ledBrightness = 0;
int switchPin = D2;


void setup() {
    pinMode(ledPin, OUTPUT);
    pinMode( switchPin , INPUT_PULLUP);
    
    Particle.variable("light", &photoReading, INT);
    

}

void loop() {
    
    int buttonState = digitalRead( switchPin );
    
      // Using a pulldown resistor we get a LOW
  // Signal when its on
  
  if( buttonState == LOW )
  {

      // Use analogRead to read the photo cell reading
  // This gives us a value from 0 to 4095
        photoReading = analogRead(photoPin);

  // Map this value into the PWM range (0-255)
  // and store as the led brightness
        ledBrightness = map(photoReading, 3000, 4095, 0, 255);

  // fade the LED to the desired brightness
        analogWrite(ledPin, ledBrightness);

  // wait 1/10th of a second and then loop
  }else{
    // otherwise
    // turn the LED Off
    digitalWrite( ledPin, LOW);
    
  }

}
Click to Expand
0

Product

An LED whose brightness changes based on the light it senses in the environment when it is turned on.

0
IoT - Simple Internet Appliance
Shivani Kannan - https://youtu.be/hdkb3GTVMQM
0

Reflection

Initially, I chose to work with a knock sensor. The sensor needed a different code; being unfamiliar with coding & circuits, I decided to start with the light sensor and experiment with the knock sensor. I also realized that clearly writing down what I wanted the code to do before coding makes things easier.

x
Share this Project

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



About

Integrate sensing into the circuits, read information from the environment around, and translate that into an LED output.