Jonathan BlantonNicholas Heidenreich
Published

Bike Speed and Tilt MEGR-3171 F. '21

We created an integrated tilt and speed sensor for a bike using magnets.

IntermediateFull instructions provided65
Bike Speed and Tilt MEGR-3171 F. '21

Things used in this project

Hardware components

Argon
Particle Argon
×2
Hall Effect Sensor, Source Output
Hall Effect Sensor, Source Output
×1
neodymium magnet
×1
Jumper wires (generic)
Jumper wires (generic)
×10
Breadboard (generic)
Breadboard (generic)
×3

Software apps and online services

ThingSpeak API
ThingSpeak API
Particle Build Web IDE
Particle Build Web IDE

Story

Read more

Schematics

Tilt Sensor Wiring

Is a simple digital tilt sensor. If the sensor is tilted it will output a digital HIGH. A particle argon is used for both, but is not available in the wiring software used,

Hall sensor wiring

D4 is the input from the sensor, GND,3v3 used to ground and power respectively. The sensor has a digital output.

Tilt Sensor Layout

Hall Sensor Layout

Code

Code for the hall sensor

C/C++
The first code measures the time difference from each time the magnet triggers the sensor to high. it will publish a warning if the speed, which is in km/hr, is below 10. This then is picked up by the second argons, seen after line 58, which checks if the driver is leaning the bike. If the driver is leaning, then it lights the LED and sends a warning back to the velocity argon 2 argons were used for this project, but could easily be combined into one.
void setup() {
//D6 is the input, GND is the ground, 3v3 is the power supply
    pinMode(D4,INPUT);
    //set D6 as input to change to high


    // set pin mode for D7 to have the if statement
    pinMode(D7,OUTPUT);
    



// 700mm wheels so .7m diameter
//float allows for decimals
Particle.subscribe("DriverTilt",danger);
const float velocity= float(1);
}
void loop() {
    int reedState = digitalRead(D4);
    static int lastReedState;
    static unsigned long lastTransition;
   

    // On a rising transition of the reed switch:
    if (reedState == HIGH && lastReedState == LOW) {
         int now = millis();
        
        // Compute time since last valid transition.
       int revolutionTime = now - lastTransition;

        // Debounce: this transition is valid only if enough time has
        // elapsed since the last valid one. thus prhibiting a double read as well as a "bouncing" effect
        //50 is the lower limit to prevent bounce 
        if (revolutionTime >= 30) {
            const float velocity= float (((.7*3.1415)/revolutionTime*1000)*3600/1000);           
            Particle.publish("velocity",String(velocity),PRIVATE);
            lastTransition=now;  
        if (velocity<=10){
            Particle.publish("warning");
        }
          
        }
    }
                 lastReedState=reedState;
                
}
            
    // Remember last state.
    
//NEED TO WORK ON PUBLISHING AND COMMUNICATION

void danger(const char* DriverTilt,const char* warning) {
    Particle.publish("LED_ON",PRIVATE);
    digitalWrite(D7,HIGH);
    delay(100);
    digitalWrite(D7,LOW);
   
}

Tilt Sensor Code

C/C++
This code establishes all the necessary functions to read the data for the tilt sensor in this project. The spots that say "API KEY" is where the user should copy in their API key from ThingSpeak to plot the data. The "if" statement in lines 27-34 reads if the tilt sensor is sending a "HIGH" output and therfore the "bike" would be leaning and then the counter is updated at each pass of the loop so long as the output is still high. The fall function is designed to potentially warn the rider if they are at risk of, you guessed it, falling. This is done by a particle subscribe from the Argon calculating velocity and if the velocity is too low for the bike to be leaning, the D7 pin LED is illuminated to warn the rider.
    ////D3 is the input, GND is the ground, 3v3 is the power supply
    
    int WarnLED = (D7);
    
    int Tilt = (D3);
    
    int Lean_cnt = 0;

void setup() {
    

    //Setting PinMode for input from tilt sensor
    pinMode(D3,INPUT);
    
    //Setting up PinMode for driver warning
    pinMode(D7,OUTPUT);
    
    Particle.subscribe("warning",fall);
    
    const String key = "DIAD3RHOIUA6W2NM";        //API Key for Thing speak integration
}

//dont need LED-on both ways with the warning from the other

void loop() {//this is an output so we can graph of how many passes of the loop occur where the bike is leaning 
    int DriverTilt = digitalRead(Tilt);
    if (DriverTilt ==HIGH) {
        Particle.publish("lean_true",PRIVATE);
        delay(2500);
        Lean_cnt = Lean_cnt + 1;
        Particle.publish("thingSpeakWrite_All", +
     "{ \"2\": \"" + String(Lean_cnt) + "\"," +
       "\"k\": \"" + "DIAD3RHOIUA6W2NM" + "\" }", 60, PRIVATE);
    }
}
//this checks if the condition published by warning in blinkmagnet is true and then runs a code to check if the driver is tilting. If true, then say fall
void fall(const char *event, const char *data){
    if (digitalRead(Tilt) == HIGH){
    digitalWrite(WarnLED, HIGH);
    Particle.publish("DriverTilt",PRIVATE);
    delay(1000);
    digitalWrite(WarnLED,LOW);
    }
    
    else{
    digitalWrite(WarnLED,LOW);
    }
}

Credits

Jonathan Blanton

Jonathan Blanton

1 project • 1 follower
Nicholas Heidenreich

Nicholas Heidenreich

1 project • 0 followers
Thanks to .

Comments

Add projectSign up / Login