loading
Picture of Arduino XMAS hitcounter
IMGP2499.JPG
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.


 
Remove these adsRemove these ads by Signing Up

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

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
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 Arduino.cc. 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.
Testing

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 : //www.youdomain.com/counter.php. I included this snippet in my wordpress blog.

$hits = file($count_my_page);
$hit = trim($hits[0]);
$hit++;
$fp = fopen($count_my_page , "w");
fputs($fp , "$hit");
fclose($fp);
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 = '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)

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.

..>python counter.py

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.

counter.py484 bytes

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.

1-40 of 45Next »
brycepruett997 months ago

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

saymowan10 months ago

Nice project! :---)

tli42 years ago
Can you change the code so that an LED Blinks when it gets a hit? Thanks. Please reply soon!
klaurens3 years ago
If I wanted to connect a Normal DC Motor to the Arduino which only has two connector pins, how would i do it??
I wouldn't recommend doing this. There are motor controllers for the Arduino that can handle the current requirements of DC motors.
Where is the code that needs to be put into the computer program?
alex_weber (author)  etylerawesome3 years ago
Take a look at step 5.
klaurens3 years ago
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)  klaurens3 years ago
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.
If I wanted to use a Piezo and an LED would the coding be much different??
alex_weber (author)  klaurens3 years ago
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.
SimonUK3 years ago
This is such a great idea, and inspired my own interpretation, which is standalone - no computer required. Have a look here:

http://www.instructables.com/id/Arduino-standalone-web-enabled-bell/
PaulCote3 years ago
Ring the bell every time someone tweets "Merry Christmas"? Not sure how, though.
Buzzsushi4 years ago
Will it work with twitter?
Did you manage to implement this with twitter? and if so would you be willing to share the code?

Thank you
alex_weber (author)  Buzzsushi4 years ago
What do you mean, ring the bell when someone replied to you on twitter?
Yes, that should be no problem.
aeboi804 years ago
I was looking for the bell and found a bunch of places at google shop - http://bit.ly/aCeJtS
moogbeatz994 years ago
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.....
 i'd like to implement this with blogger (http://ilictronix.com), 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
ReCreate6 years ago
So what does the python script do when it sees someone visited your site?
alex_weber (author)  ReCreate6 years ago
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.
XD Yes i know that part how does it trigger the bell?
alex_weber (author)  ReCreate6 years ago
It sends a signal via serial port to the Arduino. Do you mean that?
What signal(you are not being descriptive...)
alex_weber (author)  ReCreate6 years ago
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.
Ah that makes more sense now.
Maker Shed6 years ago
Wow nice Project!
akatsuki6666 years ago
and that thing with the loop (thats a reoccuring problem in alot of skeches)
akatsuki6666 years ago
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)  akatsuki6666 years ago
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
akatsuki6666 years ago
help?
luke6 years ago
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???
alex_weber (author)  luke6 years ago
accobra866 years ago
www.youtube.comGood work. my son (says proud father) made similar use of the RC servos in a Golf Robot RC. Can be found on youtube.com under this name... http://www.youtube.com/results?search_query=golfrobot+sigtuna&search_type=&aq=f
Kryptonite6 years ago
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)  Kryptonite6 years ago
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
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.
iman7 years ago
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!
cr0ybot7 years ago
Added to Arduino Group! ;D
LinuxH4x0r7 years ago
Nice job! Looks like it would get annoying after a while...
1-40 of 45Next »