Tinkerlog header image 2

Tinkerlog

Alex’ blog

Arduino XMAS hitcounter

December 4th, 2007 · 47 Comments · Arduino, sound

Christmas is coming closer, so here is my contribution to put you in the right mood.

It is a blog hitcounter, that rings a bell. Literally. It puts a smile on your face, every time someone hits your blog. And it is a great way to annoy your colleages or your girl friend.

It consists of an Arduino board, a bell, a servo and a couple of lines of code in c, python and php.

Arduino XMAS hitcounter

Materials

So what is needed?

  • Arduino Board. I have an Arduino Diecimila from Adafruits. In the meantime there are really cheap and handy clones out there, e.g. the bare bone board from Modern Devices, especially if you want to use them on a breadboard.
  • A servo motor. Any servo will do. I took an old one that was used in my former hobby.
  • A bell. Prefarably one that is small enough to shake it with the servo.
  • Two paperclips. A large one to hold the bell and a small one to build the actuator to ring the bell.
  • A website. In fact it hasn’t has to be a website or a blog. Actually everything that can be counted, will work.
  • A PC or a Mac to connect the Arduino board with the blog or website.
  • Wires.

Assembly

The bell is held by a strong paperclip. The small paperclip is used to form a kind of arm that is atached to the servo motor.

imgp2490_a.jpgimgp2505_a.jpg

Schematics

There is no real schematic. Just attach the servo motor to the Arduino. The servo has three wires:

  • yellow or orange: signal
  • red: VCC
  • brown: GND

The red and the brown one are attached to the according pins on the Arduino. The orange one is wired to pin 2. It will signal the servo in which direction to turn.

Servo connector

Software

The software stack has three parts. First part is the counter embedded in my blog. Second is the glue code, running on my mac to fetch the counter and to update the Arduino. Third is the Arduino sketch itself to control the servo motor.

This is a small php snippet to write a counter into a text file. I have included it into one of my wordpress templates. That’s not very smart, but for now my traffic is not that high so this should not be a problem.

<?php  $count_my_page = ("hitcounter.txt");
$hits = file($count_my_page);
$hit = trim($hits[0]);
$hit++;
$fp = fopen($count_my_page , "w");
fputs($fp , "$hit");
fclose($fp);
echo $hit;
?>

Here is the second part, the python snippet to fetch the counter. It opens a connection to the saved hitcounter.txt and loads the content, which is the actual value of the counter. This counter is compared with the previous fetched value. If it differs, than it has an incident to report. The delta is written as byte to the serial port and sent to the Arduino. That is done every ten seconds.

#
# fetch counter
#
import time
import urllib
import serial

# usb serial connection to arduino

ser = serial.Serial(
   '/dev/tty.usbserial-A4001JAh', 9600)
myUrl = 'http://tinkerlog.com/hitcounter.txt'

last_counter = urllib.urlopen(myUrl).read()
while (True):
  counter = urllib.urlopen(myUrl).read()
  delta = int(counter) - int(last_counter)
  print "counter: %s, delta: %s" % (counter, delta)
  ser.write(chr(ord(chr(delta))))
  last_counter = counter
  time.sleep(10)

Last part is the Arduino sketch. It is mainly based on the code that I found at the ITP Physical Computing. In the loop you see the reading of the delta value provided by the python script. If there are some rings to perform, a little state machine takes control of how far and when to move the servo arm. The servo motor is controlled with PWM, pulse width modulation. That’s the last part of the sketch. How far the servo arm turns can be adjusted with the min- and maxPulse values.

/*
 * Arduino XMAS Blog hitcounter
 * http://tinkerlog.com/
 *
 * Reference:
 * - Servo control from an analog input
 *   taken from http://itp.nyu.edu/physcomp/Labs/Servo
 *
 */
int servoPin = 2;      // control pin for servo motor
int minPulse = 1200;   // minimum servo position
int maxPulse = 1700;   // maximum servo position
int pulse = 0;         // amount to pulse the servo
int rings = 0;         // amount of rings
long nextMillis = 0;   // the time to pass before the next action
long lastPulse = 0;    // the time in milliseconds of the last pulse
int refreshTime = 20;  // the time needed in between pulses
int state = 0;         // for the state machine

void setup() {
  pinMode(servoPin, OUTPUT);  // Set servo pin as an output pin
  pulse = minPulse;           // Set the motor position value to the minimum
  Serial.begin(9600);
}

// define states for the state machine
#define PULSE_ON       0
#define WAIT_PULSE_ON  1
#define PULSE_OFF      2
#define WAIT_PULSE_OFF 3

void loop() {

  // read the rings
  if (Serial.available() > 0) {
    rings += Serial.read();
    Serial.println(rings);
  }

  // state machine to control, which action to perform
  if (rings > 0) {
    switch (state) {
      case PULSE_ON:
        pulse = maxPulse;                // send servo to max position
        nextMillis = millis() + 300;     // wait 300 ms
        state = WAIT_PULSE_ON;
        break;
      case WAIT_PULSE_ON:
        if (millis() > nextMillis) {
          state = PULSE_OFF;             // time is up
        }
        break;
      case PULSE_OFF:
        pulse = minPulse;                // send servo to min position
        nextMillis = millis() + 1000;    // wait 1 second between two rings
        state = WAIT_PULSE_OFF;
        break;
      case WAIT_PULSE_OFF:
        if (millis() > nextMillis) {
          state = PULSE_ON;              // time is up
          rings--;                       // one ring is done
        }
        break;
      }
  }

  // pulse the servo again if the refresh time (20 ms) have passed:
  if (millis() - lastPulse >= refreshTime) {
    digitalWrite(servoPin, HIGH);   // Turn the motor on
    delayMicroseconds(pulse);       // Length of the pulse sets the motor position
    digitalWrite(servoPin, LOW);    // Turn the motor off
    lastPulse = millis();           // save the time of the last pulse
  }

}

Conclusion

It is the first time, that I built something, that has moving parts. That’s my first step to bridge the gap between the virtual and the real world. And it was really easy, the code is straight forward. Also most of the parts were in my trash bin, except the bell. Putting everything together and waiting for someone to hit my blog was fun.

Side note:

  • Me, excited: “Did you hear that? There was another one!”
  • My girlfriend: “I k-n-o-w a-l-r-e-a-d-y!”

Assembled Arduino XMAS hitcounter

Action

Ok, time for some action.

Video thumbnail. Click to play.
Click To Play

BTW, the fabulous soundtrack used in this video is free and can be found here: Wikipedia Jingle Bells, performed by the Festival Speech Synthesis System’s “singing-mode”.

Links

www.flickr.com

Add this to:Del.icio.us Del.icio.us Digg! Digg

47 responses so far ↓

  • 1 R // Dec 5, 2007 at 01:30

    ding!

  • 2 JanT // Dec 5, 2007 at 08:24

    Coole Glocke! So etwas wäre ja auch interessant für unsere nightly builds ;-) Well done!

  • 3 Tom // Dec 5, 2007 at 11:48

    Pretty Cool, makes me think about all kinds of things that can be counted.

  • 4 DU // Dec 5, 2007 at 19:22

    How did you hook a servo directly to the Arduino? I thought you needed a motor controller or something to provide the amperage. Or is this a really low power servo?

  • 5 My daily bread » Arduino X-Mas Blog Hitcounter // Dec 5, 2007 at 19:54

    [...] See it working here About KASPR: « Portfolio plus Hyper Island admission work.. [...]

  • 6 Alex // Dec 5, 2007 at 20:12

    DU,
    the controller is inside the servo. Only the signal line (orange) is coming directly from the controller. Red and brown are connected to the power pins.
    Cheers,
    Alex

  • 7 Rob // Dec 5, 2007 at 20:23

    Some one has to say it, “Every time you get a blog hit an angel gets its wings.”

  • 8 Jordan // Dec 5, 2007 at 20:23

    Great stuff! Prepare to be deafened by the ding, I just found you from makezine.

  • 9 5Volt // Dec 5, 2007 at 22:20

    Really don’t know what to say….. I simply wanted to “ding !”
    Very nice.
    Ciao

  • 10 Alex // Dec 5, 2007 at 22:40

    Ok, ok, I surrender. I pulled the plug ;-)
    Thanks for “dinging” in …
    Cheers,
    Alex

  • 11 HOW TO - Make an Arduino XMAS hitcounter // Dec 5, 2007 at 23:06

    [...] HOW TO – Make an Arduino XMAS hitcounter – Link. [...]

  • 12 Adam // Dec 6, 2007 at 00:42

    I found you on Makezine too, great project and very creative. Ding!

  • 13 Festive Mood: Arduino Xmas HitCounter, Dings With Your Blog // Dec 6, 2007 at 05:32

    [...] Tinkerlog {Arduino XMAS hitcounter} [...]

  • 14 Alan Parekh // Dec 6, 2007 at 06:02

    Fantastic job!

  • 15 technabob // Dec 6, 2007 at 12:45

    every time a bell rings, a blog gets its hits…

    Here’s a fun little holiday project for you mechanically-inclined folks out there. This strange looking contraption connects to your computer and rings a little bell every time a visitor hits your website.

    With a few bucks worth of off the shelf…

  • 16 Jes1510 // Dec 6, 2007 at 15:40

    There is now a Servo Library for the arduino that would really simplify your code. Here is an example:
    http://www.arduino.cc/playground/Learning/SingleServoExample

    This is a great project! Great job!

  • 17 Jim Robert // Dec 6, 2007 at 17:40

    Hey, I linked to you in my latest blog entry, Today’s best robo-hacking howtos

    btw… love this blog

  • 18 กระดิ่งนับฮิตให้บล็อก « DailyGizmo - Gadgets, Wonderful Things vlog by C. // Dec 7, 2007 at 01:17

    [...] Ardurino XMAS hitcounter [...]

  • 19 A bell for your website door. « the impersonal blog of jamenijamjam // Dec 7, 2007 at 08:14

    [...] is TinkerLog’s newest contraption for this holidays: the bell that you can connect to your computer. This [...]

  • 20 Julklappstips: Mekanisk träffindikator at Knaskoll.se // Dec 7, 2007 at 17:03

    [...] En komplett beskrivning på bygget finns hos Tinkerlog. [...]

  • 21 Geekadelphia » Blog Counter: Uses A Real Bell // Dec 7, 2007 at 17:06

    [...] Alex over at Tinkerlog’s latest contraption, is a Christmas bell that rings every time your blog gets a hit. It consists of an Arduino board, a servo, and a couple of lines of code in c, python and php. I don’t know what any of those things are, but I do know what a bell is. [...]

  • 22 Article4us Blog » Blog Archive » DIY hit counter rings bell to welcome each visitor // Dec 9, 2007 at 10:11

    [...] Read | Permalink | Email this | Comments [...]

  • 23 Gadget & Tech News » DIY hit counter rings bell to welcome each visitor // Dec 9, 2007 at 10:17

    [...] Read | Permalink | Email this | Comments [...]

  • 24 midiwall // Dec 9, 2007 at 17:17

    Engadget found you…

    DIY hit counter rings bell to welcome each visitor

    http://www.engadget.com/2007/12/09/diy-hit-counter-rings-bell-to-welcome-each-visitor/

    “For all the details, be sure and hit the read link — and major kudos to anyone who can stand having this thing wired up and active for over 24 hours.” :)

    Nice work!

  • 25 Joe // Dec 9, 2007 at 17:48

    Great article! I’m a hardware rookie, but you have really made me want to make one of these. Does anyone have a link to a suitable servo? I wouldn’t know the first thing to look for. Also where can you buy a Arduino in the UK? There official site doesn’t list any sellers.

    Thanks!

  • 26 Alex // Dec 9, 2007 at 21:02

    Joe,
    for the servo try a search for “rc”, radio controlled. That should give you many links to shops that sell suitable servos.
    Cheers,
    Alex

  • 27 freddie // Dec 10, 2007 at 01:21

    hey, very nice… it’s like really good thinking to take a physical type door bell and blend with a virtual door. great. thanks for sharing it../freddie

  • 28 Brian // Dec 11, 2007 at 02:23

    Awesome post, thanks! I was just wiring up a servo to my own Arduino, and here you are one-upping me with the BELL! HA!

    By the way, your code syntax highlighter is very cool! Is that a Wordpress plugin? Where can I get it?

  • 29 Brian // Dec 11, 2007 at 02:47

    Duh. Found it. http://code.google.com/p/syntaxhighlighter/

  • 30 László Köntös // Dec 14, 2007 at 08:20

    Ez tök jó. Csak az alkatrészek beszerzése nehézkes. :)

  • 31 Julian // Dec 16, 2007 at 23:32

    Hi

    Can you tell me where I can get the Python libs that you used:

    # import time
    # import urllib
    # import serial

    Cheers

  • 32 Alex // Dec 16, 2007 at 23:50

    Hi Julian,

    the serial lib is called pySerial can be found at http://pyserial.sourceforge.net/
    The other libs are built-in. At least in my Python, version 2.5.1.

    Cheers,
    Alex

  • 33 Julian // Dec 17, 2007 at 21:13

    Thanks…

  • 34 every time a bell rings, a blog gets its hits - The Gizmo Guide - The Guide to the newest Electronics // Dec 19, 2007 at 17:22

    [...] With a few bucks worth of off the shelf parts, an Arduino board, a USB cable and a little code, you too can have one for your website, thanks to this detailed “how-to” article over on Tinkerlog. [...]

  • 35 Alex // Dec 26, 2007 at 00:33

    Hm.. you can probably do this without polling from the python snippet. From the PHP code, you can call a program directly every time there is a hit. See the command exec – http://us2.php.net/function.exec

    Anyway, cool. :)

  • 36 Alex // Dec 26, 2007 at 10:21

    Hi Alex,
    that would only work, if I host my blog on my own machine, right?
    Cheers,
    Alex

  • 37 YAB » Blog Archive » [Arduino] BBVA blogs-blinker // Jan 11, 2008 at 00:54

    [...] Arduino XMAS hitcounter: same idea, bringing the remote server processus, and moving a motor if you need more fun [...]

  • 38 aiosphere » Hit Counters // Mar 12, 2008 at 01:45

    [...] a fun idea for ambient/desk gadgets.  Electromechanical counters, which make a satisfying click.  Ringing a bell, playing a chime, or a  small robotic or electromechanical element which makes a signature [...]

  • 39 Mau // May 7, 2008 at 01:56

    Hi, can you help me to build one for mi blog?? pleeease!!
    I don’t understand very well python, and i don’t know if I can use with my blog

    htt://toolex.blogspot.com

  • 40 Alex // May 7, 2008 at 07:35

    Hi Mau,
    I think Blogger, your blogging platform, wont allow you to run server side php. That may be the biggest problem.
    Cheers,
    Alex

  • 41 Hack/Mod » Blog Archive » Applause Machine // Nov 23, 2008 at 19:59

    [...] would also be interesting to hook up to a hit counter for your [...]

  • 42 Alt Fn :: 1337 » Blog Archive » Arduino Project #1: Notifier // Jun 27, 2009 at 15:21

    [...] by mushing together my Arduino with an LED toy/lamp thingy and some inspiration from the Arduino Xmas bell and the Physical Gmail Notifier [...]

  • 43 jjoggo's me2DAY // Jun 29, 2009 at 06:43

    쪼꼬팬더의 생각…

    이거 재밌을거 같아요~ í•´ë³´ê³  싶네요 ã…‹…

  • 44 Alt Fn :: 1337 » Blog Archive » How to: Arduino Notifier // Jul 4, 2009 at 13:53

    [...] I found online and all code used in this “How to” is based upon the code from the arduino xmas bell and the arduino gmail [...]

  • 45 On the 10th Day of Christmas Learning Technology Give to You… | Technology Enhanced Learning // Dec 10, 2009 at 16:52

    [...] instructions on how to make a somewhat geeky but slightly Christmasy pointless thing: the Arduino XMAS hitcounter [scroll to bottom of instructions for [...]

  • 46 adil // Jan 17, 2010 at 17:36

    lush. we have a typewriter you can email if you want to have a go, but i’m having that, “i wish i had had that idea” with this – great work.

  • 47 Fresh Objects » Arduino is an easy way to create fresh objects // Jan 25, 2010 at 17:15

    [...] Website Visitor Blinker – Christmas Bell [...]

Leave a Comment