I'm working on a Hack the Holidays project where I need to add an ambient light sensor to a Seeed Xiao BLE to control the brightness of Neopixel LEDs.
I decided to test the sensor on another Xiao (SAMD21) before trying to use it in the project.
The sensor that I'm using is the Grove Light Sensor v1.1. The sensor uses the LS06-S phototransistor to produce a linear analog output relative to illuminance, which ranges from 0 to the supply voltage. I'll use an A-to-D converter on analog pin A0 to convert that voltage.
As a display, I'm going to use a Grove LED Bar v2.0. I'll use the map() function to translate the A/D range into the LED Bar bins (levels).
Since these are Grove sensors, I'm going to use a Xiao Grove Shield to connect them.
A quick demo of the sensor operating:
Xiao_Light_sensor.ino
#include <Grove_LED_Bar.h> Grove_LED_Bar bar(3, 2, 0, LED_BAR_10); // Clock pin, Data pin, Orientation void setup() { // nothing to initialize Serial.begin(115200); bar.begin(); bar.setGreenToRed(true); } void loop() { int value = analogRead(A0); value = map(value, 0, 640, 0, 10); Serial.println(analogRead(A0)); Serial.println(value); bar.setLevel(value); delay(100); }