Projects / Bike tail light with brake flash

Bike tail light with brake flash

Intermediate2.5 hGuided buildReviewed

Build it step by step

0 of 6 done
  1. XIAOMT3608

    Wire the power

    Charge module to boost converter to XIAO, with the boost set to 5.0 V first.

  2. XIAO4.98V

    Wire the sensor and LEDs

    Connect the LIS3DH, four LEDs and light sensor as in the table.

  3. XIAO

    Find the forward axis

    Print x, y and z while pushing the board forward, and set the axis and sign in the code.

  4. XIAO

    Test braking

    Roll the bike and stop quickly, or shake the board to mimic slowing.

  5. XIAO

    Tune

    Change BRAKE and the 1.5 second hold until it flashes on real braking only.

  6. XIAO8.1 mA

    Mount

    Fix the board in a small box under the saddle with the LEDs facing back.

In short

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

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
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 pinPowers the board
MT3608 VOUT-XIAO GNDGround
LIS3DH VCC, GNDXIAO 3V3, GNDPower
LIS3DH SDA, SCLXIAO D4, D5I2C
XIAO D0 to D3Four 220 Ω resistors then four red LED anodesBrake and tail LEDs
Red LED cathodesXIAO GNDCommon return
LDRXIAO 3V3 and A10 (D10)Top of the light divider
10 kΩ resistorD10 and GNDBottom 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.

CheckExpected
Daylight, cruisingLEDs off
Dusk, cruisingSteady dim tail light
BrakingBright flashing for at least 1.5 seconds
StoppedSteady 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 →

Ask Lark AI

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.