Scenario Two - Controlling Light Automatically.

Scenario 2 – Controlling Lights Automatically.

[Temporary URL - https://www.tinkercad.com/things/5v8rhDfpVtq]

The goal here is to turn on the light bulb automatically when the light falls below a given level.

Figure 26. TinkerCAD Circuits 'Controlling Lights' scenario


The components in the figure above are:

·      An Arduino Uno board

·      A breadboard

·      Cables

·      A photoresistor (sensor)

·      Resistor 

·      Lightbulb (output component)

The Arduino is programmable, so it can make decisions based on the conditions supplied to it by a sensor. Here, we switching on the light when the reading from the photoresistor drops below a certain level. The Arduino has a series of ‘pins’, some which take in analog readings, and others take digital readings. Other pins supply either 3.3 Volts or 5 Volts of power to components – in this case a light.

The breadboard allows connections to be made between components without having to solder them together. Each row of the breadboard is connected, so any wires connected on the same row stays connected. 

The resistor in the circuit is to used to reduce current flow, and adjust signal levels.

Click on the ‘Start Simulation” button.

Click on the Photoresistor

and move the slider, and observe what happens to the light.

Stop the simulation and click on the code editor. See if you can work out how to change the thresholds in the code so that light comes on at lower and higher levels of light.

Click on the "Light Sensor and bulb"

Change the code so the light bulb shines at a different level. You can do this by changing the “int threshold” value.

int lightPin = 4; //define a pin for Photo resistor

int threshold = 10;

 

void setup(){

   Serial.begin(9600); //Begin serial communication

   pinMode(2, OUTPUT);

 

}

 

void loop(){

   Serial.println(analogRead(lightPin));

 

   if(analogRead(lightPin) > threshold ){   

       digitalWrite(2, LOW);

       Serial.println("high");

   }else{

       digitalWrite(2, HIGH);

       Serial.println("low");

   }

 

   delay(1000);

}

Note that when you run the code, you can look at the ‘serial data’ being generated by the sensor signal. Click on “Serial Monitor” on the left under the code. Note that you can see both the raw numbers and a plot. As you change the values in the light sensor, you will see the numbers and plot change.

Figure 27. TinkerCAD Circuits serial monitors


Complete and Continue