Use LDR with XIAO SAMD21

Use LDR with XIAO SAMD21

A light dependent resistor (LDR) is a type of resistor that resistance changes according to the intensity of light. It can be used in applications such as street lighting systems, burglar alarm systems, light intensity measurement systems, etc.

This tutorial shows how to use LDR with XIAO SAMD21 development board.


1.XIAO SAMD21 + USB-C cable1
3.10 kΩ resistor1

Circuit diagram

The pin of LDR is connected to the D1 (A1) pin on the XIAO SAMD21 board. 10 kΩ pull-down resistor is added between GND and the D1 (A1) pin.

LDR with XIAO SAMD21 (Circuit diagram)

Project setup

For project setup, the PlatformIO Core CLI tool is used. Make sure you have installed it.

  • Create a new directory for project and navigate to it:
mkdir xiao_project && cd xiao_project
  • Run the following command to initialize a new project:
pio project init --board seeed_xiao


The analogRead function reads an analog input and returns a number between 0 and 1023, which is printed to the serial port.


#include <Arduino.h>

#define SENSOR_PIN 1

void setup()

void loop()
    unsigned long sensorValue = analogRead(SENSOR_PIN);


Build the project and upload the program to the development board:

pio run --target upload


Connect XIAO SAMD21 via USB to a PC. Open the Serial Monitor to see analog input readings.

Testing LDR with XIAO SAMD21

Leave a Comment

Cancel reply

Your email address will not be published.