Introduction: Arduino XMAS Hitcounter

Picture of Arduino XMAS Hitcounter

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.

It consists of an Arduino board, a bell, a servo and a couple of lines of code in c, python and php. Most parts are fairly common and should be easy to get.

Step 1: Tools and Materials

Picture of Tools and Materials
So what is needed?

  • Arduino Board. I got an Arduino Diecimila from Adafruits. In the meantime there are really cheap and handy clones out there, e.g. the really 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. Preferably 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.
  • Wires to connect the servo with the Arduino.
  • A website. In fact it has not 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.
You may need an soldering iron, if the wires can not be connected directly to the Arduino.

Step 2: Hardware Assembly

Picture of Hardware 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.

Note, that you want to bent the paperclip that holds the bell in a way, that already a little shaking generates a ding.

Step 3: Schematics

Picture of 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 (5V and GND). The orange one is wired to pin 2. It will signal the servo in which direction to turn.

You may want to solder small connectors to the wires if the wires do not fit directly into the Arduino or the servo.

Step 4: Programming the Arduino

Picture of Programming the Arduino

If you are new to the Arduino, it is a small board, fully assembled with a AVR microcontroller. It is well suited for hacking and interacting with your environment. Many things that are hard with microcontrollers are rather easy with Arduino. Some of the advantages:

  • no need for a separate programming device (programmer)
  • comes with an integrated development environment (IDE)
  • runs on any platform, Windows, Mac, Linux.
  • easy connection to your PC with USB
  • hardware is open source (but the name Arduino is not)
  • has a great community
More information can be found at the official Arduino website. Be sure, to check out John's Arduino instructable for further details on how to get started with Arduino.

What does the software do?

The small piece of software that gets uploaded to the Arduino, controls the servo. It receives single bytes via the serial connection over the USB cable. If it receives a value of 5, it moves the servo arm five times forth and back. So the max value to send is 255.

Program the Ardiuno

So I assume you have downloaded and installed the latest Arduino IDE from For now it is version 0010 Alpha.
To drive the servo more comfortably you have to download a library. You can find it on the Arduino Playground. Unzip it and put the folder in .../arduino-0010/hardware/libraries/.

  • Attach the Arduino to your PC with the USB cable.
  • Open the IDE and start a new sketch. Sketch is Arduino speak for program. Select File -> New.
  • Select the appropriate serial device (Tools -> Serial Port). This depends on your environment, for me it is /dev/tty.usbserial-A4001JAh.
  • Download the attached source file and paste it into the new sketch. Hit the save button.
  • Hit the verify button. This compiles your sketch into a hex file that can be transferred to your Arduino.
  • Hit the upload button to transfer your sketch to the Arduino.

Now your hitcounter is ready for some action. Let's see if it works.

  • Hit the serial monitor button.
  • Select the text box next to the send button.
  • Hit the tab key and send it.
  • By now the servo arm should move forward and back.
Phew. That was the hardest part. For now you can send a byte to the Arduino and the servo waves at you. Next is to find something that you want to trigger the bell.

We are almost done.

Step 5: Make It a Hitcounter

To make it a hitcounter for your website, we need two small code pieces. One two create and take care of the counter and a second to fetch the value of the counter and to send it to the Arduino.

Note: If you are not familliar with Python or PHP, the scripts can be easily ported to your favorite programming language.

The counter

Here is a small PHP script, that reads a value from a file (hitcounter.txt), increments it and writes it back to the file. That is all, that is needed. This file can be saved as counter.php on your server for example. You can then trigger a count with you webbrowser pointing to http : // I included this snippet in my wordpress blog.

$hits = file($count_my_page);
$hit = trim($hits[0]);
$fp = fopen($count_my_page , "w");
fputs($fp , "$hit");
echo $hit;

The glue code

This next code snippet is used to fetch the counter. I used Python but anything should work. It opens an HTTP connection and fetches the hitcounter.txt. If the value has changed since the last fetching, the diff is calculated and pushed to the Arduino. This is done every ten seconds until you interrupt the script with crtl-c.
Adapt the myUrl and the serial connection below to your needs.

# fetch counter
import time
import urllib
import serial

# usb serial connection to arduino

ser = serial.Serial('/dev/tty.usbserial-A4001JAh', 9600)
myUrl = ''

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)

I could not upload files with php extension, so you have to rename the counterphp.txt to counter.php.

If your Arduino is still attached to your PC, then start the Python script to fetch your hitcounter.


and you should see the output of the counter. If you point your browser to the URL of your counter.php and hit reload, you should hear your hitcounter ringing.

Yes, now we are done. Time to lean back and enjoy our work.

Step 6: Conclusion

Picture of Conclusion

It is the first time, that I built something, that has moving parts. That is the 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.

Hope you enjoyed it.


Tonkadragon (author)2016-11-02

Why is it that I have to get an app from the app store to read something I download from here if it is in my computer there should be no problem reading it?

DorT1 (author)2015-12-29

Great project!

You can can use this free tool for quickly generate schematics and code snippets:

brycepruett99 (author)2014-12-30

The python script crashes when the counter gets to 10. Any solutions?

saymowan (author)2014-09-23

Nice project! :---)

tli4 (author)2013-03-28

Can you change the code so that an LED Blinks when it gets a hit? Thanks. Please reply soon!

klaurens (author)2011-12-29

If I wanted to connect a Normal DC Motor to the Arduino which only has two connector pins, how would i do it??

Snortimer (author)klaurens2012-06-18

I wouldn't recommend doing this. There are motor controllers for the Arduino that can handle the current requirements of DC motors.

etylerawesome (author)2012-04-29

Where is the code that needs to be put into the computer program?

alex_weber (author)etylerawesome2012-04-30

Take a look at step 5.

klaurens (author)2011-12-30

Any place that sells Remote controlled Helicopters, cars, etc. Radio Shack will have them too...

klaurens (author)2011-12-29

If I wanted to connect a Normal DC Motor to the Arduino which only has two connector pins, how would i do it??

alex_weber (author)klaurens2011-12-30

You can't because the motor needs too much current. You have to use an H-bridge or a motor shield from adafruit or something like that.

klaurens (author)alex_weber2011-12-30

If I wanted to use a Piezo and an LED would the coding be much different??

alex_weber (author)klaurens2011-12-30

Piezo and LED can be driven directly from the Arduino. Maybe look for an Arduino tutorial to learn how to deal with LEDs and Piezos.

SimonUK (author)2011-12-17

This is such a great idea, and inspired my own interpretation, which is standalone - no computer required. Have a look here:

PaulCote (author)2011-11-26

Ring the bell every time someone tweets "Merry Christmas"? Not sure how, though.

Buzzsushi (author)2010-12-04

Will it work with twitter?

liamviney (author)Buzzsushi2011-03-02

Did you manage to implement this with twitter? and if so would you be willing to share the code?

Thank you

alex_weber (author)Buzzsushi2010-12-05

What do you mean, ring the bell when someone replied to you on twitter?
Yes, that should be no problem.

aeboi80 (author)2010-10-28

I was looking for the bell and found a bunch of places at google shop -

moogbeatz99 (author)2010-10-14

how would i set up this hit counter thing with a google site? would it work at all? im not planning on actually doing this instructable, i might when i get spare time and money, but i would like to actually know if it would work.....

psychofish25 (author)2009-11-11

 i'd like to implement this with blogger (, but blogger doesn't support php :(  is there a way I could write to a file with javascript?

hearing this every 15 seconds might get annoying :P

ReCreate (author)2009-07-07

So what does the python script do when it sees someone visited your site?

alex_weber (author)ReCreate2009-07-07

It fetches a small text file from my site. This text file carries the number of hits. If the number of hits is higher than last time, then trigger the bell.

ReCreate (author)alex_weber2009-07-08

XD Yes i know that part how does it trigger the bell?

alex_weber (author)ReCreate2009-07-08

It sends a signal via serial port to the Arduino. Do you mean that?

ReCreate (author)alex_weber2009-07-08

What signal(you are not being descriptive...)

alex_weber (author)ReCreate2009-07-08

Take a look at the python snippet:
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)

It computes the delta between the last counter value and the current counter value. Say, the old value was 112, now it is 115. Next the delta, 3 is send as byte over the serial port to the Arduino. The Arduino reads the byte and rings the bell three times.

ReCreate (author)alex_weber2009-07-08

Ah that makes more sense now.

Maker Shed (author)2009-06-26

Wow nice Project!

akatsuki666 (author)2009-02-09

and that thing with the loop (thats a reoccuring problem in alot of skeches)

akatsuki666 (author)2009-02-08

my arduino barly takes any codes wthout saying error -.- and this one seems to say that... in function ' void loop () ' : error: ' refresh ' is not a member of ' servo '

alex_weber (author)akatsuki6662009-02-08

Hi Akatsuki666, try deleting the line with refresh. If you are using the latest version of the ArduinoIDE, the method "refresh" is no longer available. Cheers, Alex

akatsuki666 (author)2009-02-08


luke (author)2008-12-27

hahah i love it , 5* and faved!

i have to ask , i didnt see it in your description. but what is your your macbook playing ?? it sounds like you have got the termnal to sing! useing the saay command???

accobra86 (author)2008-12-08 work. my son (says proud father) made similar use of the RC servos in a Golf Robot RC. Can be found on under this name...

Kryptonite (author)2008-11-22

Is it possible to hook it up to anything else? say... when some body talks to u on msn and ur in the next room, cud u hook that up? or instructables comments? Just idea, but this wud be soooo neat to try out for them.

alex_weber (author)Kryptonite2008-11-22

Hi Kryptonite, yes, that would be possible. You would have to modify the python script to fetch an RSS feed for example. Most messaging systems or comments are availble as RSS feed. If a new message or comment is available in the feed, the python script will receive it and notify the Arduino/bell. For the comment feed on instructables, go to your profile and enable your private feed. Cheers, Alex

Kryptonite (author)alex_weber2008-11-22

Sweet. thanks for the quick reply. I'm seriously thinking about making this now, cuz I play this game, and I can't tell if somebody want to talk to me I don't know. Then an hour later I go see, and the person has logged off. thanks, Kryptonite.

iman (author)2008-04-06

wow this is sweet I saw this a while back but I ignored it because I didn't have an arduino but now that I got one this will be my first project :) Thanks!

cr0ybot (author)2008-02-10

Added to Arduino Group! ;D

LinuxH4x0r (author)2007-12-16

Nice job! Looks like it would get annoying after a while...

PocketSized (author)2007-12-16

This is such a cool project using the Arduino. I'm going to have to order one of these after christmas. Can't wait, and great Instructable.

Toulouse (author)2007-12-16

awesome idea! it's kinda like the digg counter, but if you had this in your house, you could know when people visit your blog.

GorillazMiko (author)2007-12-16

looks difficult. great instructable!

alex_weber (author)GorillazMiko2007-12-16

Hi GorillazMiko, it is not as difficult as it may look. It is definitely easier than programming microcontrollers directly. Arduino is a great way to get involved with micros. Cheers, Alex

Brennn10 (author)2007-12-16

Thats great! Nice job!

About This Instructable




More by alex_weber:64pixelsArduino XMAS hitcounterSynchronizing Fireflies
Add instructable to: