Project tutorial
Hamster Odometer

Hamster Odometer © GPL3+

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

  • 494 views
  • 0 comments
  • 8 respects

Components and supplies

Apps and online services

About this project

"H"...like history and like hamster

How many times are you waking up in the night and it seems to hear your libble buddy running and running...and running again?

So it came a simple question: "How far can cover my hamster nightly? Why not to measure it?"

The first idea was using an encoder connected on the wheel, but encoders are too expensive for a such of application ;-) so i thought to hall sensor properties: i could glue some magnets to the wheel and let the Arduino counts how many times the magnets are passing under the hall sensor. That's it!

Basics mathematics

Let's take our school books with elementary formulas that show the irrational relationship between diameter and circumference: C = π x d . This formula could be enough if we use only one magnet, but to have more precision i put 4 magnets so every transition we have to count the division by 4: space = C/4

Choosing the components

The Arduino Uno was not a 100% weighted technical choice, i simply already had in my DIY box ;-) and the number of input/output are enough for the purpose.

For the hall sensor module (KY003) i searched a hall sensor with a digital output (easy to count the transitions) that cn be supplied with same voltage (5V) from Arduino Uno board.

For the display, needed to visualise the hamster's progress, i choose a simple and cheap I2C LCD character display module that has 4 lines of 20 LCD characters.

Attached to the display there is a I2C module that convert signals I2C to the 16 pins: it's easier to use only 4 wires (SCL, SDA, VDD, GND). On the I2C module there is a variable resistance to modify the display contrast.

Coding

For this tutorial,it is necessary to download and install the LiquidCrystal_I2C library. LiquidCrystal_I2C is a library of Arduino which enables serial LCD 20x4 connect with Arduino. To be able to interface the serial LCD with arduino, you will have to download this library and save it into your Arduino's libraries.

To detect the the single magnet transition i can't use a simple if(S == HIGH) {cycle++} inside the loop because the uC is faster than the hamster :D, so i needed to detect the single transition magnet using 2 states of hall sensor: HIGH (magnet near sensor), LOW (magnet far from sensor). Only a complete transition (from LOW to HIGH and from HIGH to LOW) decide the "+1".

Construction

First use glue to attach magnets to the wheel and band together the hall sensor to a wood stick (chopsticks are ok).

When sensor is fixed, take your time to adjust the sensor position moving the wood sticks and keeping the distance between magnet and sensor around 1-2 cm. Pay attention to have the sensor centred on the magnet when it's gettin under. (Fig. 2)

Bread board has been used only to ease to supply two boards: hall sensor and display (Fig. 3). To have more detailed info on connection look at the Fritzing schematics

To avoid waisting energy and to limit bothering the hamster's nightly run, i added a timer in order to switch off the display if the wheel is not in use. So after every magnet transition display is lighted for 5 seconds and then it has been switched off until next transition.

Have fun!!

Next steps

I'm working on a version 2.0 of hte project where the hamster's progress is logged into a database. With the database you could know when is mostly running duting the night.

Stay tuned!!!

Code

Code for Hamster OdometerArduino
#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");
}

Schematics

Hamster odometer
hamster_odometer_urBAadIjiC.fz

Comments

Similar projects you might like

Hamster Fitness Tracker

Project in progress by Peter Gullberg

  • 1,612 views
  • 0 comments
  • 4 respects

Bicycle Odometer and Speedometer with 99 Lap/Period Recorder

Project in progress by alan_dewindt

  • 2,487 views
  • 7 comments
  • 11 respects

Arduino and AC Devices - Automatic Lights

Project tutorial by Ahmed Hamdy

  • 22,490 views
  • 8 comments
  • 50 respects

Hall effect sensor with Arduino

Project tutorial by AMAL NS

  • 9,905 views
  • 1 comment
  • 23 respects

Control your LED with Cortana

Project tutorial by Mohamed Emam

  • 8,682 views
  • 7 comments
  • 21 respects

The Magnetic Field and RGB Tester

Project tutorial by Kutluhan Aktar

  • 3,535 views
  • 0 comments
  • 14 respects
Add projectSign up / Login