Bike tail light with brake flash
Build it step by step
0 of 6 doneWire the power
Charge module to boost converter to XIAO, with the boost set to 5.0 V first.
Wire the sensor and LEDs
Connect the LIS3DH, four LEDs and light sensor as in the table.
Find the forward axis
Print x, y and z while pushing the board forward, and set the axis and sign in the code.
Test braking
Roll the bike and stop quickly, or shake the board to mimic slowing.
Tune
Change BRAKE and the 1.5 second hold until it flashes on real braking only.
Mount
Fix the board in a small box under the saddle with the LEDs facing back.
How do you build a bike tail light with brake flash?
An accelerometer detects braking and makes a rear LED array flash brighter, with a light sensor to turn it on at dusk. You need Seeed XIAO RP2040, LIS3DH accelerometer, LED 5 mm red × 4, Resistor 220 Ω × 4, LDR Photoresistor, Resistor 10 kΩ, Li-Po battery 3.7 V 500 mAh, TP4056 module with protection (USB-C) and MT3608 Boost Module. It takes 6 steps and about 150 minutes.
- Level
- Intermediate
- Time
- About 150 min
- Parts
- 9
Parts list
- Development BoardsBuy
- SensorsBuy
- SemiconductorsBuyLED 5 mm red × 4
- PassivesBuyResistor 220 Ω × 4
- SensorsBuy
- PassivesBuyResistor 10 kΩ × 1
- BatteriesBuy
- PowerBuy
- PowerBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| LiPo battery + and - | TP4056 module B+ and B- | Charged over USB-C by the module |
| TP4056 OUT+ and OUT- | MT3608 VIN+ and VIN- | Battery to boost converter |
| MT3608 VOUT+ (set to 5.0 V first) | XIAO 5V pin | Powers the board |
| MT3608 VOUT- | XIAO GND | Ground |
| LIS3DH VCC, GND | XIAO 3V3, GND | Power |
| LIS3DH SDA, SCL | XIAO D4, D5 | I2C |
| XIAO D0 to D3 | Four 220 Ω resistors then four red LED anodes | Brake and tail LEDs |
| Red LED cathodes | XIAO GND | Common return |
| LDR | XIAO 3V3 and A10 (D10) | Top of the light divider |
| 10 kΩ resistor | D10 and GND | Bottom of the light divider |
Code
Arduino IDE with the "Raspberry Pi Pico/RP2040" boards package by Earle Philhower, board "Seeed XIAO RP2040". Install "Adafruit LIS3DH" and "Adafruit Unified Sensor". Set the MT3608 to exactly 5.0 V on a multimeter before connecting the XIAO, and use a protected LiPo and a TP4056 module with battery protection. The 220 Ω resistors keep each LED to about 6 mA, within the RP2040's pin limit. Test on a stationary bike first and keep a conventional reflector and a proper rear light for road use; this is a hobby add-on.
Runs on a virtual Seeed XIAO RP2040 in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
#include <Wire.h>
#include <Adafruit_LIS3DH.h>
#include <Adafruit_Sensor.h>
Adafruit_LIS3DH lis = Adafruit_LIS3DH();
const byte LEDS[4] = { D0, D1, D2, D3 };
const byte LDR = D10;
const float BRAKE = 1.8; // m/s2 of slowing down that counts as braking
const int DARK = 600; // LDR reading below this means dusk
float slow = 0, fast = 0;
unsigned long brakeUntil = 0;
void setLeds(int level) { for (byte i = 0; i < 4; i++) analogWrite(LEDS[i], level); }
void setup() {
for (byte i = 0; i < 4; i++) pinMode(LEDS[i], OUTPUT);
Wire.begin();
lis.begin(0x18);
lis.setRange(LIS3DH_RANGE_4_G);
}
void loop() {
sensors_event_t e; lis.getEvent(&e);
float a = e.acceleration.y; // forward axis: change to x or z, or the sign, to suit your mounting
fast = 0.7 * fast + 0.3 * a; // smoothed to ignore bumps
slow = 0.995 * slow + 0.005 * a; // tracks tilt
float decel = slow - fast; // positive when slowing down in the forward direction
if (decel > BRAKE) brakeUntil = millis() + 1500;
bool dusk = analogRead(LDR) < DARK;
if (millis() < brakeUntil) {
setLeds((millis() / 80) % 2 ? 255 : 90); // flash bright
} else if (dusk) {
setLeds(60); // steady tail light
} else {
setLeds(0);
}
delay(20);
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Daylight, cruising | LEDs off |
| Dusk, cruising | Steady dim tail light |
| Braking | Bright flashing for at least 1.5 seconds |
| Stopped | Steady dim tail light at dusk |
If it does not work
Flashes on every bump
Raise BRAKE or smooth the reading more.
Never flashes
Wrong axis or sign. Print the values while braking.
Always on at noon
Adjust DARK or check the LDR divider.
Board resets
Check the MT3608 is at 5 V and the battery is charged.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the bike tail light with brake flash?
Seeed XIAO RP2040, LIS3DH accelerometer, LED 5 mm red × 4, Resistor 220 Ω × 4, LDR Photoresistor, Resistor 10 kΩ, Li-Po battery 3.7 V 500 mAh, TP4056 module with protection (USB-C) and MT3608 Boost Module.
How long does the bike tail light with brake flash take to build?
About 150 minutes for a intermediate maker, in 6 steps.
How do I know the bike tail light with brake flash works?
Daylight, cruising: LEDs off. Dusk, cruising: Steady dim tail light. Braking: Bright flashing for at least 1.5 seconds.
Flashes on every bump: what should I check?
Raise BRAKE or smooth the reading more.
Never flashes: what should I check?
Wrong axis or sign. Print the values while braking.
Always on at noon: what should I check?
Adjust DARK or check the LDR divider.
Board resets: what should I check?
Check the MT3608 is at 5 V and the battery is charged.
