Arduino Stopwatch Experiment

I’ve been working my way through the lessons in the SunFounder LCD Ultrasonic Relay Sensor Electronic Bricks Starter Kit. The kit comes with a manual, and the early experiments have pretty detailed steps with electronics diagrams.

But by the time you get to the stopwatch experiment (lesson 13) there are no more diagrams, so I was left to figure out how to connect the 4-digit 7-segment display. My display had the part number SMA420564, and it had 12 pins. I couldn’t find a spec sheet for it, to understand which pins referred to which segment/digit. After a bit of trial and error, I figured it out:

Pin Diagram for SMA420564
Pin Diagram for SMA420564

The way I figured it out which pin mapped to which was to adapt the example program that came with the kit to light all segments at once (i.e. “8.”) and then plug/unplug wires until I figured out which was which. I don’t really know the pin numbers, but as long as you know which digit/segment they light, that’s enough. The pins d1, d2, d3, and d4 correspond with each digit, with d1 being the first on the left. The pins with capital letters correspond with the segments A-G of the 7-segment character. The naming convention appears to be quite standardized. The diagram below shows which letter corresponds with which segment:

7-segment-display-labeled

Here’s a wiring schematic to show which pins of the Arduino each pin goes to. Note that in the diagram below, the LED display is rotated 90º clockwise:

Timer_Schematic
Schematic for Experiment 13 in Sunfounder Ultrasonic Kit for Arduino

I used Upverter to create the diagram, and you can access it directly on the Upverter site.

With everything wired, it was time to try the code. The kit I had came with one of those small CDs, so I actually downloaded it from the Sunfounder web site. It’s lesson #13 in the Ultrasonic Kit for Arduino here. (NOTE: The original URL doesn’t work, but I found a ZIP of the entire set of lessons for the Ultrasonic Kit, and also posted the source for lesson #13 as a Gist.) When I tried to validate the stopwatch, it failed with the following error:

Timer1 was not declared in this scope

Turns out that there are some C++ libraries that the Sunfounder example code relies on, which weren’t in my environment. I found this blog post that talks about the error, and there’s a link to download the TimerOne library. After downloading it I wasn’t really sure what to do with the .h and .cpp files. I tried placing them in the same directory as my .ino file, but that didn’t help. Eventually I figured out that you have to import it via the Arduino IDE (Sketch>Import Library>Add Library… and then select the folder that contains the TimerOne.cpp and TimerOne.h files.

With that done, I was able to burn the program to my Arduino, and now have a happy stopwatch:

stopwatch

6 thoughts on “Arduino Stopwatch Experiment

  1. Hi Antun,

    Thanks a lot for publishing this great description how to make the stopwatch run. The other stuff I found wasn’t useful.

    Kind regards
    Nicolas

  2. Hello Antun

    Thank you for your blog, which I found very helpful.

    I bought a Sunfounder Lab Started Basic Kit for Beginners – Ardruino Uno R3, on eBay. It included a little manual and small CD-ROM.
    I found the tiny print in the manual difficult to read, so I contacted them. They were were very helpful and sent me a PDF version which I find much more convenient.

    In the manual for the kit that I bought, the Stopwatch is Lesson 14. On page 41, there is a photo of the underside of the SMA420564,
    with all the pins labelled, however I found your ‘Pin Diagram for SMA420564’ more useful.

    In my copy of the manual, Lesson 13 is for a One Digit 7-Segment Display. When I reached that lesson, I learned how the 7 segments of the character are mapped (ie A-G) from https://en.wikipedia.org/wiki/Seven-segment_display

    Regarding lesson 14 in my version of the manual, I learned from your blog: “The pins d1, d2, d3, and d4 correspond with each digit, with d1 being the first on the left.” However, I found your wiring schematic a bit disconcerting as my understanding is each segment should be protected by a current-limiting resistor.
    When I searched for more info, I found this ==> http://learn.parallax.com/4-digit-7-segment-led-display-arduino-demo
    which seems to confirm that segments should be protected. Hence, I used 8 x 220 ohm resisters to protect the 7 segments, plus decimal point.

    Then I followed the instructions in the manual, for connecting the pins of the SMA420564
    to the Digital (PWM) pins 2-13 of the Adruino.

    When I compiled the sketch that Sunfounder provides, it reported a fatal error. Hence, I found your tip about a missing library very helpful. The line in the code is : #include

    I downloaded the library from ==>> https://code.google.com/p/arduino-timerone/downloads/list
    then I extracted the files into a folder called TimerOne Library. When I tried to import the library via the Arduino IDE : Sketch > Import Library > Add Library
    it failed, apparently because ‘space’ is an illegal character. I renamed the folder TimerOne_Library. When I tried to import again, it failed and I ended up with a line called ‘examples’ in the list of libraries.
    Apparently it failed because I pointed at the contents, rather than the directory name itself.
    So, I tried again, finally TimerOne_Library appeared in the list of libraries.
    Then Sunfounder’s example sketch compiled without any errors. Finally, I uploaded it to my Uno and it ran first time. Success!
    Now I’m left with ‘examples’ in my list of Libraries and I naturally would like to know how to remove it.

    Anyway, thanks again.

  3. All right, I’ve discovered how to remove a library. It’s as simple as deleting a directory. So, look in the Arduino IDE folder, then open the Libraries folder and delete the library that you wish to remove. If only other puzzles were so easy :~/

Leave a Reply to Nicolas Cancel reply

Your email address will not be published. Required fields are marked *