Things used in this project

Hardware components:
Cutout cortado grande
Punch Through LightBlue Bean
Tail light
Mfr 25frf52 1k sml
Resistor 1k ohm
1M ohm resistor
0.1uf capacitor
Hand tools and fabrication machines:
09507 01
Soldering iron (generic)
Screw driver
Needle-nose pliers


Taillight new1


This sketch automizes turning a bike taillight ON and OFF. 

The bike light will turn on when the taillight is in vertical position and it moves.

This example code is in the public domain.

// When acceleration change goes beyond this threshold, the LED will blink.
#define THRESHOLD 100
#define BUTTON_PIN 0

AccelerationReading previousAccel;
uint16_t ledReading = 0;
bool ledIsOn;
// Timer counts how long the taillight has been still
int timer = 0;
//String state;

void setup() {
// Initial reading
previousAccel = Bean.getAcceleration(); 

void loop() {
// Check if the taillight is ON or OFF
ledReading = getLedReading();
if(ledReading == 0){
ledIsOn = false;
ledIsOn = true;

// Get the current acceleration with a conversion of 3.9110-3 g/unit.
AccelerationReading currentAccel = Bean.getAcceleration();

// Find the difference between the current acceleration and that of 200ms ago.
int accelDifference = getAccelDifference(previousAccel, currentAccel);

// Update previousAccel for the next loop. 
previousAccel = currentAccel;

// Check if the conditions are right to turn ON the taillight
if(accelDifference > THRESHOLD// Taillight is moving
&& !ledIsOn // LED is OFF
&& currentAccel.xAxis < -200){// Taillight is in a vertical position
// state = "Active";
timer = 0;
// Turn the LED ON
// Check if the conditions are right to turn OFF the taillight
else if(accelDifference < THRESHOLD // Taillight is still && ledIsOn){// LED is ON // state = "Inactive"; timer ++; if(timer > 30){
 // Turn the LED OFF
 timer = 0;
// state = "Other";
timer = 0;

// String output = String();
// output = output + "State: " + state + "\tTimer: " + timer + "\tLED Reading: " + ledReading + "\tAccel Diff: " + accelDifference + "\tAccel X Axis: " + currentAccel.xAxis;
// Serial.println(output);


// This function calculates the difference between two acceleration readings
int getAccelDifference(AccelerationReading readingOne, AccelerationReading readingTwo){
int deltaX = abs(readingTwo.xAxis - readingOne.xAxis);
int deltaY = abs(readingTwo.yAxis - readingOne.yAxis);
int deltaZ = abs(readingTwo.zAxis - readingOne.zAxis);
// Return the magnitude
return deltaX + deltaY + deltaZ; 

// This function simulates a button press to turn the light ON or OFF
void pressButton(int milliseconds){
// Simulate a button press for a duration 
digitalWrite(BUTTON_PIN, LOW);
// Set BUTTON_PIN as a high impedance input to simulate a "button release" and allow the physical button to function again

// We use a low-pass filter connected to LED_STATUS_PIN to monitor the LED state
// When LED_STATUS_PIN is above a certain value it corresponds to the LED being OFF
uint16_t getLedReading(){
uint32_t reading = 0;
// Take the average of 8 readings over a period of time
for(int i = 0; i<8; i++){
reading += analogRead(LED_STATUS_PIN);
reading = reading/8;
return reading;


Ray Kampmeier

Are you Ray Kampmeier? Claim this project and add it to your profile.

This is mine
Posted by 11014907 917762441613247 2463536654633089299 o Punch Through


Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback


Similar projects you might like

Web-controlled BoE-Shield Robot with the LightBlue Bean[+]
  • 830
  • 22

Full instructions

Have you ever wanted to control a robot from a web browser? Use the LightBlue Bean[+] (LBB[+]) to control a BoE-Shield Robot using BLE.

  • 1,093
  • 19

FitBit-type Bluetooth radiation sensor for tracking radiation exposure (for working around X-rays in the hospital, or just life in general).

Handy BLE app / Bean Car
  • 4,187
  • 29

Work in progress

Remote control bean powered car driven with an iPhone running the Handy BLE app.

El Wire T-shirt: Connected to a LightBlue Bean
  • 285
  • 6

Work in progress

A custom Internet connected shirt using a LightBlue Bean to light up the shirt's El Wire graphics.

Data Pyrotechnics
  • 552
  • 7

Full instructions

What happens when you strap a LightBlue Bean to some fireworks?

Water Detection System
  • 2,888
  • 23

Full instructions

Send email alerts with LightBlue Bean using Node-RED on Raspberry Pi

Add projectSign up / Login