TCS Pune

RP2040 — Automatic Light Controller

Embedded Project Builder

RP2040 — Automatic Light Controller

Use a light sensor with hysteresis to control an output. Program and practical guide for RP2040.

Problem statement

Use a light sensor with hysteresis to control an output. The implementation must operate inside the electrical, timing and memory limits of the exact RP2040 device.

System design

Circuit & Wiring Studio

Exact target required

Drag components. In Connect mode, select one pin and then another pin.

Rich media learning

Print lab worksheet

Instructor media is being prepared for this project.

Project execution and evidence

Log in to start this practical, save readings and submit evidence.

Log in to start

Recommendations & learning path

Bookmark project

Equivalent and related projects

Components required

ComponentQtyNotes
LDR and divider resistor1Verify voltage/current rating
Breadboard and insulated jumpers1Inspect before power
USB data/programming cable1Must support data
Digital multimeter1Lab tool
Current-limited bench supply1Lab tool

Pin-connection table

Controller pinConnected deviceDevice pinVoltage / notes
Board supplyTarget VCCConfirm exact board input and regulator
Board groundModule groundShared reference unless isolated
ADC inputSelected peripheral pins3.3 V maximum; confirm pin mux
Protected outputDriver inputNever power high-current load from GPIO

Project files

Source-code files

Instructor file pending.

PCB and Gerber files

Instructor file pending.

Enclosure design files

Instructor file pending.

Target-specific program

Toolchain: · Language:

// RP2040 — Pico SDK light project
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/adc.h"
#define OUT_PIN 15
#define IN_PIN 14
int main(){stdio_init_all();gpio_init(OUT_PIN);gpio_set_dir(OUT_PIN,GPIO_OUT);gpio_init(IN_PIN);gpio_set_dir(IN_PIN,GPIO_IN);gpio_pull_up(IN_PIN);adc_init();adc_gpio_init(26);adc_select_input(0);absolute_time_t next=get_absolute_time();puts("TCS PROJECT READY");while(true){if(absolute_time_diff_us(get_absolute_time(),next)<=0){next=make_timeout_time_ms(100);uint16_t sample=adc_read();bool input=!gpio_get(IN_PIN);gpio_put(OUT_PIN,input||sample>2048);printf("TYPE=light ADC=%u INPUT=%dn",sample,input);}}}

Testing and observations

  1. Inspect wiring with power disconnected.
  2. Power from a current-limited supply.
  3. Build with the exact target and warning level enabled.
  4. Flash the program and capture startup output.
  5. Measure supply and relevant I/O signals.
  6. Inject one safe input or communication fault.
  7. Repeat the functional test for at least 20 cycles.
  8. Record code version, board revision and results.
TestExpectedInstrumentAcceptance criteria
Supply railWithin target-board tolerance
Idle currentBelow approved project limit
Functional outputMatches the stated objective
Fault responseSafe and recoverable
Repeated test20 of 20 cycles pass

Viva questions

Why was this pin selected?

Explain pin capability, boot function and voltage limit.

What fails if timing blocks?

Other work and fault response become delayed.

How was current limited?

Use a resistor, driver and protected supply as required.

What evidence proves operation?

Code version, measurements, serial output and repeated tests.

Prototype safety

Verify voltage, polarity, GPIO limits and isolation before power-up. Use current limiting and disconnect power before wiring changes.

Guided practical

Complete learning and build guide

Beginner2–3 hoursSensors and data acquisition

Objective

Use a light sensor with hysteresis to control an output.

Required knowledge

  • RP2040 pin functions and voltage limits
  • Embedded C/C++ fundamentals
  • Safe breadboard wiring
  • Serial debugging and measurement

Power and current calculation

Calculate total current from the board, sensor/module and output load. GPIO supplies only logic-level current; motors, relays, buzzers and high-current LEDs require a correctly rated driver and protected external rail. Start with current limiting and verify actual idle and peak current.

Libraries and build procedure

Libraries: Pico SDK hardware_gpio, hardware_adc, hardware_pwm and selected bus library

  1. Create a project for RP2040 using Pico SDK with CMake.
  2. Select the exact MCU/board and clock configuration.
  3. Paste the complete program and change only the documented pin constants.
  4. Install only the libraries listed for this project.
  5. Build with warnings enabled and resolve every error.
  6. Connect the board with outputs disconnected and upload/program it.
  7. Confirm startup diagnostics, then connect the protected load.
  8. Record the final binary size and test evidence.

Complete source code

// RP2040 — Pico SDK light project
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/adc.h"
#define OUT_PIN 15
#define IN_PIN 14
int main(){stdio_init_all();gpio_init(OUT_PIN);gpio_set_dir(OUT_PIN,GPIO_OUT);gpio_init(IN_PIN);gpio_set_dir(IN_PIN,GPIO_IN);gpio_pull_up(IN_PIN);adc_init();adc_gpio_init(26);adc_select_input(0);absolute_time_t next=get_absolute_time();puts("TCS PROJECT READY");while(true){if(absolute_time_diff_us(get_absolute_time(),next)<=0){next=make_timeout_time_ms(100);uint16_t sample=adc_read();bool input=!gpio_get(IN_PIN);gpio_put(OUT_PIN,input||sample>2048);printf("TYPE=light ADC=%u INPUT=%dn",sample,input);}}}
SectionExplanation
ConfigurationSelects the exact target pins, baud rate and safe defaults.
InitialisationConfigures peripherals and reports startup state.
Non-blocking loopSchedules input, processing and output without long delays.
Fault handlingDetects invalid data or timeout and selects a safe output.
DiagnosticsPrints measurements and state for repeatable testing.

Expected Serial Monitor output

TCS RP2040 PROJECT
SELFTEST: PASS
STATE: READY
VALUE: 0
FAULTS: 0
PointExpectedToleranceInstrument
Board supplyRated board logic rail±5%DMM
GPIO LOW0 VWithin datasheet VOLDMM/oscilloscope
GPIO HIGH3.3 VWithin datasheet VOHDMM/oscilloscope
Signal timingProgrammed period±5%Oscilloscope/logic analyser
SignalTest pointReference
Digital/PWM outputConfigured output pinClean transitions inside 3.3 V limits
Serial outputUART TXStable frames at configured baud
Sensor busSDA/SCL or SCK/dataValid idle level, timing and acknowledgements

Troubleshooting decision tree

SymptomCheckCorrective action
Board not detectedUSB cable, driver, boot mode and targetUse data cable and correct programmer settings
No outputSupply, ground, pin mapping and safe defaultCorrect one item with power disconnected
Wrong readingsReference voltage, scaling and sensor rangeCompare raw value and DMM result
Random resetsCapture supply during peak loadImprove supply/decoupling and separate load power
Communication failsLogic voltage, pull-ups, mode/address and baudMatch both endpoints and use common reference

Extension challenges

  • Replace blocking timing with a state machine.
  • Add calibrated measurement and retained configuration.
  • Add serial commands with bounds checking and timeout.
  • Measure active and sleep current.
  • Create a PCB-ready pin and connector table.
  • Add watchdog recovery and diagnostic export.

Project-report guidance

Include objective, exact MCU/board, schematic, pin table, BOM, complete code, compiler/tool versions, expected and measured readings, waveform evidence, fault injection, corrections, limitations and extension results.

Ready when you are

Need help with your laptop or computer?

Send us the model and fault details. Start your repair request online or contact TCS Pune directly.

WA