The final result of this project will be a device that counts the distance I swim in the pool. The initial version calls out the distance on a speaker using a speech synth library. I was going to use an Arduino sound shield that I had from a previous project, but I couldn't' get it to work again with a nano. 

I also looked into building an underwater speaker, and I may do that at some point, but there's nothing commercially available that's remotely cheap enough. 

The current version uses the speaker but also shows the distance on a LED display.

I'm also thinking about buying a cheapo smart watch off of Alibaba and writing a android app that counts and displays the app. I don't know what sensors are available or if I can even load an app on a cheapo smart watch. That's a project for another day. 

  1. Test ultrasonic sensor on breadboard. Done. Works
  2. Write code to detect swimmer coming and going and count laps. Assume 45-1:30 times for 50 yards. Count each lap as 50 yds. Write output as serial

Psuedocode - real code and fritzing diagram is in github:

0) Wait until momentary pushbutton starts (look into interrupts)
     Start_time = millis()
     Last_lap_time = millis()
   1) When Distance <100 cm for more than .25 sec and milis() - last_lap_time > 40 sec. 
      2) Lap completed.
          Increment lap_count
          Yards = lap_count * 50
     While distance <100 cm for more than 0.25 sec
        Announce yards
        Serial print yards
        Serial print elapsed time
        Serial print average time for 100 yds
    While end
    Last_lap_time = millis()

Note: This code isn't exactly what I wrote, but it's what I thought up at the time.

3. Add Amp and battery.

4. Added a reset switch and an on/off switch. It all works great on the bench on USB power. 

5. Built a simple enclosure out of a repurposed takeout container. I cut a hole in it for the ultrasonic sensor "eyes" and for the speaker. I cut holes in the top for the switches. This is the part of the project that I suck at. I can solder and program, but all I do is make a big mess with hot glue.

Let's take it to the pool and test for real. 

5. The good news is the sensor works fine and it counts laps, at least for a while. The bad news is that it's not nearly loud enough. Also, it failed somewhere between 10 and 20 minutes in after running for hours with no problems on USB power.

6. Added battery Booster. Seems to help. 

7. Added Adafruit 7 segment display w/backpack


Tried it in the pool. In the bright sunshine, it's hard to read the LED but not impossible. Most of the time I swim at or before dawn so it will be no problem. The biggest problem is that it stopped recording laps after 350 yards (7 laps). I think it might be because a water droplet got on the ultrasonic detector.

To minimize that kind of thing happening again, I cut out a piece of plastic and hot glued it above the "eyes" and now it looks like a person wearing a baseball cap. Will test again tomorrow.


It basically works, but I have a bug in my logic somewhere. Three or four times during my swim it appeared to freeze but several laps later it caught back up. It should never be able to count laps and not update the display so there's obviously a bug. Shocking, I know. 


Read more »