Franzduino
Published © GPL3+

Hamster Odometer

Taking an Arduino Uno, one display, one Hall sensor and four magnets, i measured how many meters run my little hamster named "Ciambella."

BeginnerFull instructions provided4 hours4,125
Hamster Odometer

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
I2C LCD 2004 Display Module (20 characters x 4 lines)
×1
WINGONEER Hall sensor KY-003
×1
Neodym magnet
×4

Software apps and online services

Windows 10
Microsoft Windows 10
Arduino IDE
Arduino IDE

Story

Read more

Schematics

Hamster odometer

Code

Code for Hamster Odometer

Arduino
#include "Wire.h" // For I2C
#include "LCD.h" // For LCD
#include "LiquidCrystal_I2C.h" // Added library*


//Set the pins on the I2C chip used for LCD connections
//ADDR,EN,R/W,RS,D4,D5,D6,D7
LiquidCrystal_I2C lcd(0x3F,2,1,0,4,5,6,7); // 0x27 is the default I2C bus address of the backpack-see article
// Connessione su Arduino UNO di LCD I2C
// --> SDA = A4
// --> SCL = A5

int Led = 13 ; // define LED Interface
int SENSOR = 10 ; // define the Hall magnetic sensor interface
int val_N = HIGH; // define NEW value of sensor
int val_P = HIGH; // define PREDECESSOR value of sensor
long counter = 100000; // circa 23s
unsigned long transitions = 0 ;
const float pi = 3.1415926535;
const float r = 0.135;    // radius in meters
int n_mag = 4;            // number of magnets attached to the wheel
float d = (2*r*pi)/n_mag; // calculation of the distance done between two magnet transitions is automatic
float distanza_tot = 0;

void setup()
{ 
  /* Setup for KY-003 Hall magnetic sensor module */
  pinMode (Led, OUTPUT) ;    // define LED as output interface
  pinMode (SENSOR, INPUT) ;  // define the Hall magnetic sensor line as input
  
  /* Setup LCD Display 20 x 4 */
  lcd.begin (20,4); // 20 x 4 LCD module
  lcd.setBacklightPin(3,POSITIVE); // BL, BL_POL
  lcd.setBacklight(1);

  // Greetings
  lcd.setCursor(0,1);
  lcd.print("Hello Ciambella!");
  delay(2000);
 
  lcd.clear();
  lcd.print("Passaggi: ");
  lcd.setCursor(10,0);
  lcd.print(transitions);
  lcd.setCursor(0,1);
  lcd.print("Distance: ");
}

void loop()
{
  //evaluate sensor
  val_N = getval();
  // check if counter reached zero: that means 5 seconds are passed by, 
  // so the display can be switched off to save power and do not put the hamster out.
  if(counter < 0){
    lcd.setBacklight(0);
    counter = -1;
  }else{
    lcd.setBacklight(1);
  }

  // Check the PREVIOUS (val_P) and the NEW (val_N) values
  // if val_P == HIGH and val_N == LOW the magnet is getting under the sensor: sensor's LED is on.
  // Please note that the logic is inverse.
  if (val_P == HIGH && val_N == LOW)
  {
    // magnet under sensor
    // delay (10);
    // Update TOGGLE variables
    val_P = val_N;
    counter--;
  }else if (val_P == LOW && val_N == HIGH){
    // If val_P == LOW and val_N == HIGH means that the sensor is passed by, 
    // so the counter has to go on by one
    transitions++;
    update_lcd();
    //align states
    val_P = val_N;
    //reset lcd timer for extend display lighting
    counter = 23000; // circa 5s
  }else{
    counter--;
  }
}

/* ------SUBROUTINES------ */
int  getval(){
  int sensore = digitalRead (SENSOR);
//  lcd.setCursor(0,2);
//  lcd.print(sensore);
//  lcd.setCursor(0,3);
//  lcd.print(d);
  return sensore;
}

void update_lcd(){
  lcd.setCursor(10,0);
  lcd.print(transitions);
  lcd.setCursor(10,1);
  distanza_tot = transitions*d;
  lcd.print(distanza_tot);
  // Update TOGGLE variables
  lcd.setCursor(15,1);
  lcd.print("m");
}

Credits

Franzduino
2 projects • 3 followers
Contact

Comments

Please log in or sign up to comment.