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.
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.
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.
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 = 'https://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 * https://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!”
Action
Ok, time for some action.
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
- Arduino, official site
- Arduino servo control
- Real Bare Bone Board at Modern Devices
- Arduino Diecimila at Adafruits
More pictures at Flickr.
ding!
LikeLike
Coole Glocke! So etwas wäre ja auch interessant für unsere nightly builds ;-) Well done!
LikeLike
Pretty Cool, makes me think about all kinds of things that can be counted.
LikeLike
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?
LikeLike
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
LikeLike
Some one has to say it, “Every time you get a blog hit an angel gets its wings.”
LikeLike
Great stuff! Prepare to be deafened by the ding, I just found you from makezine.
LikeLike
Really don’t know what to say….. I simply wanted to “ding !”
Very nice.
Ciao
LikeLike
Ok, ok, I surrender. I pulled the plug ;-)
Thanks for “dinging” in …
Cheers,
Alex
LikeLike
I found you on Makezine too, great project and very creative. Ding!
LikeLike
Fantastic job!
LikeLike
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!
LikeLike
Hey, I linked to you in my latest blog entry, Today’s best robo-hacking howtos
btw… love this blog
LikeLike
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!
LikeLike
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!
LikeLike
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
LikeLike
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
LikeLike
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?
LikeLike
Duh. Found it. http://code.google.com/p/syntaxhighlighter/
LikeLike
Ez tök jó. Csak az alkatrészek beszerzése nehézkes. :)
LikeLike
Hi
Can you tell me where I can get the Python libs that you used:
# import time
# import urllib
# import serial
Cheers
LikeLike
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
LikeLike
Thanks…
LikeLike
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. :)
LikeLike
Hi Alex,
that would only work, if I host my blog on my own machine, right?
Cheers,
Alex
LikeLike
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
LikeLike
Hi Mau,
I think Blogger, your blogging platform, wont allow you to run server side php. That may be the biggest problem.
Cheers,
Alex
LikeLike
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.
LikeLike
Hi,
Great tutorial, everything was going flawlessly until we hit a rock with the python setup. We have no experience with python and just wondering if you could send the python files (setup) and the arduino files? If you can we would really appreciate it, we’ll share with you what we’re connecting this with, it’s a really cool/interesting setup we want to make. Not sure if it matters but we’re setting this up on a Macintosh system.
Thanks a lot!
LikeLike
Cool idea, really. I know its way more awesome in Hardware, but here my software solution:
http://www.flyerman.org/design/webdesign/ein-besucher/?lang=en
LikeLike