Skills Dev II - Lulin Shan

Made by Lulin Shan

Created: November 14th, 2021

0

Outcome

I learned and practiced how to use inputs (button, switch, and potentiometer) and sensors (photoresistor and FSR). For the exercise of combining sensors and inputs, I made a connected sensor device that reminds me to pour the boiled water in the kettle to the thermos.  

0

Getting Inputs

Exercise 1

Combine a switch and potentiometer. The switch should turn on and off the light while the potentiometer will fade up and down the light (but only when it’s on).

I wired a button to the circuit created in the tutorial “using a potentiometer”. I started with reusing the code in that tutorial and defined a new pin that I would place a button on. I reorganized the code related to analog readings/values to the if (buttonState == LOW) condition. Following the 3 step-by-step tutorials is really helpful and it makes the practice exercise less daunting!

0
// define a pin that I'll place pot on
 int potPin = A5;
 
 // create a variable to hold the pot reading
 int potReading = 0;
 
 // define a pin I'll place an LED on
 int ledPin = D2;
 
 // create a variable to store the LED brightness
 int ledBrightness = 0;
 
 //define a pin I'll place a button on
int buttonPin = D3;
 
 void setup() {
     
    // for input, I define the pushbutton as an input-pullup
    // this uses an internal pullup resistor to manage consistent reads from the device
    
    pinMode( buttonPin, INPUT_PULLUP ); // sets pin as input
    
     // set up the LED for output
     pinMode( ledPin, OUTPUT );
     
     // create a cloud variable of type integer
     // called 'light' mapped to photoCellReading
     Spark.variable( "pot", potReading );

}

void loop() {
    
    // find out if the button is pushed or not by reading from it
    int buttonState = digitalRead( buttonPin );
    
    // when the button is pushed, we will get a LOW signal
    // when the button is not pushed, we'll get a HIGH
    
    // set the LED on or off
    if( buttonState == LOW )
    {
        // turn the LED on
        digitalWrite ( ledPin, HIGH );
        
        // use analogRead to read the potentiometer reading
        // This gives us a value from 0 to 4095
        potReading = analogRead( potPin );
        
        // map this value into the PWM range (0-255) and store as the led brightness
        ledBrightness = map( potReading, 0, 4095, 0, 255 );
        
        // fade the LED to the desired brightness
        analogWrite( ledPin, ledBrightness );
        
        // wait 1/10th of a second and then loop
        delay(100);
        
        }else{
        // otherwise, turn the LED off
        digitalWrite( ledPin, LOW );
    }
}
Click to Expand
0
Getting Inputs - Exercise
Lulin Shan - https://youtu.be/kgG5wPNIoUU
0

Using a Photoresistor

Exercise 1

Modify the program so that the LED shows the opposite. When there is little light, it will be bright and when there’s lots of light, it will be off.

I changed the PWM range from 255 to 0 in map( photoCellReading, 0, 4095, 255, 0 ). 

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;

void setup() {
    
    // set up the LED for output
    pinMode( ledPin, OUTPUT );
    
    // create a cloud variable of type interger called 'light' mapped to photoCellReading
    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 (255-0) and store as the led brightness
    ledBrightness = map( photoCellReading, 0, 4095, 255, 0 );
    
    // fade the LED to the desired brightness
    analogWrite( ledPin, ledBrightness );
    
    // wait 1/10th of a second and then loop
    delay(100);

}
Click to Expand
0
Using a Photoresistor - Exercise 1
Lulin Shan - https://youtu.be/IbyVPJgKJoo
0

Exercise 2

Instead of fading your light, why not turn it on or off when the light reaches certain levels.

I set the threshold as 1000. If the light levels are below 1000, turn the LED on, otherwise turn it off.     

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;

void setup() {
    
    // set up the LED for output
    pinMode( ledPin, OUTPUT );
    
    // create a cloud variable of type interger called 'light' mapped to photoCellReading
    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 );
    
    // find out if light levels are below 1000
    // set our LED on or off
    if ( photoCellReading < 1000){
        ledBrightness = 255;
    }else {
        ledBrightness = 0;
    }
    
    // fade the LED to the desired brightness
    analogWrite( ledPin, ledBrightness );
    
    // wait 1/10th of a second and then loop
    delay(100);

}
Click to Expand
0
Using a Photoresistor - Exercise 2
Lulin Shan - https://youtu.be/JjwBhkwBPVg
0

Practice Exercise: Combining Sensors and Inputs

User Scenario

I like hot drinks when I’m working at home. I have a thermos that helps keep boiled water warm for a longer time, which saves the waiting time whenever I want a cup of hot drink. However, I often forget to pour the boiled water into the thermos before it gets cold:( 

 I hope to design a connected sensor device that reminds me to pour the boiled water in the kettle to thermos.

0

Input

Force sensitive resistor(FSR): sense the weight of the electric kettle with water
Switch: turn the system on or off 

0

Output

LED: When the kettle is sensed with more than 0.5L water inside and the switch is on, turn LED on

0
// Define a pin that I'll place the FSR on, remember to add a 10K Ohm pull-down resistor too.
int fsrPin = A0;

// Create a variable to hold the FSR reading
int fsrReading = 0;

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

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

// Define a pin I'll place a switch on
int switchPin = D3;

void setup()
{
    // for input, I define the switch as an input-pullup
    // this uses an internal pullup resistor to manage consistent reads from the device
    
    pinMode( switchPin, INPUT_PULLUP ); // sets pin as input
    
    // Set up the LED for output
    pinMode(ledPin, OUTPUT);
  
    // Create a cloud variable of type integer called 'force' mapped to fsrReading
    Particle.variable("force", &fsrReading, INT);
  
}

void loop()
{
    // find out if the switch is thrown to the wired terminal ot not or not by reading from it
    int switchState = digitalRead( switchPin );
    
    // when the switch is thrown to the wired terminal, we will get a LOW signal
    // when the button is thrown to the unwired terminal, we'll get a HIGH signal

    if( switchState == LOW )
    {
        // Use analogRead to read the fsr reading
        // This gives us a value from 0 to 4095
        fsrReading = analogRead(fsrPin);
        
        // find out if pressure levels are below 3000
        // set our LED on or off
        if ( fsrReading < 3000){
            ledBrightness = 0;
        }else {
            ledBrightness = 255;
        }
        
        // fade the LED to the desired brightness
        analogWrite(ledPin, ledBrightness);

        // wait 1/10th of a second and then loop
        delay(100);
        
    }else{
        // otherwise, turn the LED off
        digitalWrite( ledPin, LOW );
    }
}
Click to Expand
0
Combining Sensors and Inputs
Lulin Shan - https://youtu.be/I5NvjJiYnVY
0

Next Steps

It would be helpful to measure the pressure from the kettle with 0.5L water. Due to the size and shape of FSR, it was difficult for me to place the FSR under the center of the kettle base to measure the actual pressure. For this exercise, I set the threshold as 3000 for testing purpose.

I would like to use a temperature sensor to measure the water heat. For example, if the water temperature is more than 80 °C and the switch is on, turn the LED on.

0

Reflection

This week’s tutorials helped me become more familiar with using inputs and sensors. I also became more comfortable in wiring and coding. I found it challenging to create a circuit on the breadboard from scratch. For this exercise, I referred to Fritzing circuit diagram in tutorials as a starting point, then add a second component into the mix. This approach smoothed the wiring progress a lot.

If I were to do it again, I would use a temperature sensor that tells me about the water in the electric kettle is warm. Additionally, it would be great if the LED can be placed on my desk. Otherwise, this connected device would not help me a lot if the LED is set by the kettle on the kitchen counter.  

x