Skill Dev II
Made by Sohyun Jin ·
Made by Sohyun Jin ·
Working with Inputs and Sensors
Created: November 8th, 2023
At first, I conducted tests to check if the LED brightness could decrease to the desired level using a photoelectronic sensor (SEN-09088).
For the photoelectronic sensor, which has a known value range of 0-4095, I needed to adjust the mapping values to match the amount of light exposed to the sensor. By reducing the Particle Cloud's stream reading values to a range of 2500-3600, I was able to visually distinguish the brightness of the light. (Please enlarge the video to the full screen below)
// 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;
void setup() {
pinMode(ledPin, OUTPUT);
Particle.variable("light", &photoCellReading, INT);
}
void loop() {
// 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
ledBrightness = map(photoCellReading, 2500, 3600, 0, 255);
// fade the LED to the desired brightness
analogWrite(ledPin, ledBrightness);
// wait 1/10th of a second and then loop
delay(100);
}
Click to Expand
In the second test, I added a Flex Sensor (SEN-08606) as the second component. The scenario I had in mind was as follows: when the day is bright, the LED lighting should either turn off or maintain a low level of brightness. When the day becomes dark, the LED lighting should become brighter, and the user should be able to customize this using a bending switch. When the day is bright, the amount of light should decrease, so I set the mapping values to (3000, 1700) in reverse.
When the LED lighting is dim (20-100), bending the flex sensor inward increases the light, and when the LED lighting is bright (255), bending the flex sensor outward decreases the light. There were cases where the mapped LED lighting size resulted in a negative value when exceeding the limits set in the mapping. In such cases, the amount of light decreased significantly, so I intentionally set the mapping values for the flex sensor to be (1000, 250).
int ledPin = D2;
int ledBrightness = 0;
int ledBrightnessB = 0;
int ledBrightness1 = 0; // variable from photoCell sensor
int ledBrightness2 = 0; // variable from bending sensor
int ledState = LOW;
int photoCellPin = A0;
int photoCellReading = 0;
int bendPin = A5;
int bendReading = 0;
void setup() {
//set the LED to output
pinMode(ledPin, OUTPUT);
Particle.variable( "photolight", &photoCellReading, INT );
Particle.variable( "bent", &bendReading, INT );
Particle.variable( "light", ledBrightness ); // console variables connecting.
}
void loop() {
// Use analogRead to read the photo cell reading
// This gives us a value from 0 to 4095
photoCellReading = analogRead( photoCellPin );
bendReading = analogRead( bendPin );
ledBrightness1 = map( photoCellReading, 3000, 1700, 0, 255); //LED brightness 0-255(adjust the value range)
ledBrightness2 = map( bendReading, 1000, 1250, 0, 255);
ledBrightness = (ledBrightness1 + ledBrightness2) / 2;
analogWrite( ledPin, ledBrightness );
delay( 100 );
}
Click to Expand
This project is only accessible by signed in users. Be considerate and think twice before sharing.
A hands-on introductory course exploring the Internet of Things and connected product experiences.
Working with Inputs and Sensors