This tutorial will cover how to create lights that turn on when it gets dark. We will be using the Arduino microcontroller along with the SwissCHEESE shield to make it easier to code.
This tutorial assumes that you have a working environment setup with the Arduino IDE and Cardublock library. See here for more information.
- When wiring, make sure that the Arduino USB wire is not plugged into the computer.
- Insert the SwissCHEESE shield onto the Arduino board.
- Attach a jumper wire to a LED of your choosing. The other end of the wire should be attached to pins O0 on the SwissCHEESE board.
- Attach a jumper wire to a SwissCHEESE Light Sensor. The other end of the wire should be attached to pins I1 on the SwissCHEESE board.
Open up Arduino IDE, and go to Tools –> CarduBlock Edu to open up the Cardublock user interface.
- The light sensor takes in values from 0 to 1023. The dimmer the light, the higher the value. We will first create a variable called “darkness”, by going to Variables/Constants –> Set Integer Variable. Next we will set the variable’s value to LDR, which is found in CAROBOT SwissCHEESE –> LDR.
- Now we want to make a decision, so first add an if/else statement from Control –> If/Else. Add the greater than test found in Tests –> ‘>’ to the test section of the If/Else Statement. From Variables/Constants, add Standard Integer, and Standard Integer Variable, and rename like below. What we are doing is checking if the darkness is greater than 800.
- Now we are going to decide what we do with the If/Else statement. So if the darkness exceeds 800, then we will turn the LED on, otherwise, we will turn it off. Drag the LED blocks from CAROBOT SwissCHEESE –> LED and adjust the status sections like below.
- Upload the code to the Arduino.
You should have something that looks like this: