Here's a code sample that triggers the relay when the ambient light in the room changes. You can set a threshold for how dark or bright you want it to get, so it's easy to customize for a lot of different applications.

The one gotcha with using light sensors to turn lights on and off is that when it's dark enough to trigger the sensor, and you use the sensor to turn on a light, then it's no longer dark.. and so the light turns off again. It's sort of a chicken-and-egg problem. So in practice, you may want to use the light trigger for turning lights ON, then use a different trigger for turning them OFF.

How it Works

The magic happens with the digital write pin A0 to low block, located under ADVANCED > PINS

What it's doing is sending a signal through the white wire you hooked up to pin A1 and either connecting or disconnecting it to GND. The power switch relay can read the difference, and that's how it knows to switch the outlets on or off.

Once you've customized your code, click the Download button at the bottom of the screen. Plug your Circuit Playground into your computer via the USB port. The lights on the face will turn green (if they don't, click the tiny "reset" button in the center of the board) and a drive will appear on your computer called CPLAYBOOT

To program your Circuit Playground, drag the file you downloaded onto this drive and the code will start running.

If you're having trouble, head over to the MakeCode Beginner Guide for more tips and troubleshooting.

This guide was first published on Apr 21, 2020. It was last updated on Apr 21, 2020.

This page (Light Activation) was last updated on Apr 17, 2021.

Text editor powered by tinymce.