In this lesson, we use a photoresistor to control the light to turn on in dark environments automatically.
After creating a new project, click on the Simulation icon on the left side of the page to enter the Simulation page.
Connect the LED module to P0 and photoresistor module to A0. Connect the VCC and GND to 3.3V and GND respectively. The photoresistor can sense the intensity of light. The dimmer the light, the smaller the value of the photoresistor, and vice versa.
Create 1 variable called phoVal by clicking Create variable under the Variables category.
The reading value of the photoresistor is assigned to the variable phoVal and then you can print it to the Debug Monitor via print block.
In the Sensor Kit category, drag an LED module block below the print block.
Since the read value of potentiometer ranges 0-4095 and the value of the brightness ranges 0-100, a map block is needed to map 0-4095 to 0-100. Get a map block to put it into LED module block.
Modify the values as shown below. In addition, phoVal block comes from the Variables category. Add a delay block below the LED module block.
Now, go to the Simulation page and click on the Run button, then drag the slider to simulate the read value of photoresistor. You can see the brightness of LED changing with it. Now save your project, connect the Bluetooth and click Flash to compile and download the code.