Speedometer effect on an LED ring by using the accelerometer or the gyro

Hi

I need some help :o ! I am working on a project and I am trying to create a speedometer effect on an LED ring by using the accelerometer or the gyro sensor of an Arduino 101.
The Arduino will be mounted on a bicycle and the idea is while I am moving the pixels on the led ring are lighting up one by one based on how much I accelerate. And while I decelerate it is happening the opposite and turning off the pixel one by one.
I don't want an accurate result I just want something to happen on the LED ring while I m biking.
First of all is this possible to be done by using one of these 2 sensors of the Arduino?

I know that with the gyro I can get the angular velocity and I was thinking if it is possible to transform it to linear velocity and with some maths make it happen. But I am not sure if this is possible.

Then I thought that maybe by using the accelerometer and implementing the velocity type in the code (ay is the raw values from the accelerometer for the Y axis):

unsigned long StartTime = 0;
unsigned long CurrentTime = millis();
unsigned long ElapsedTime = CurrentTime - StartTime;

int velocity;

velocity = V0 + ay+( ay * ElapsedTime/1000);
Serial.print("\t");
Serial.print(velocity);
Serial.println();

But it didn't work because the values were increasing as the time passes.

Any ideas or thoughts will be much appreciated :slight_smile:

Post complete code! My guess is that your algorithm for the velocity is wrong but I don't know where you get the values from.

Keep in mind that the sensor has measuring errors that together with rounding errors may sum up so you must have a way to set the zero point somehow.

Thank you for the reply! I have attached the code.

Accelerometer_Y_axis_Velocity.ino (10.3 KB)

  unsigned long StartTime = 0;
  unsigned long CurrentTime = millis();
  unsigned long ElapsedTime = CurrentTime - StartTime;

That code always results in ElapsedTime to be equal the current value of millis(), so the milliseconds since startup.

If you use that value to multiply with acceleration and sum that up, you get way to large numbers. You should calculate the time since the last velocity addition, so move the initialization of StartTime to setup() and in loop() set StartTime to CurrentTime after you calculated ElapsedTime.

Think about it this way: you are trying to measure the acceleration of a bicycle while sitting in a dragster car doing a 7-second run on the quarter-mile track.

That is 1g acceleration.

If your accelerometer is mounted even 1 degree off horizontal then the error accumulates very fast. Your bike speedometer will be wrong by several thousand kph in just a few minutes.

Count wheel rotations instead. You can probably buy the reed switch sensors very cheaply if you look. Or another type of sensor may work better for you.

Sory for the late reply!Thank you very much for your help!