Introduction: DIY Bedwetting Alarm
A bed-wetting alarm can be a useful tool to help children who are late (6 years or older) to stay dry overnight. By signalling the moment that the bedwetting start, a child is more likely to recognise it next time. First discuss with a doctor or urologist before starting to use a device like this!
We needed one and looking at the reviews I was not impressed with what's available commercially, so I had a look whether it's possible to make something better and/or cheaper.
The concept is simple: sound an alarm when something gets wet. In addition, it should be small, safe and the batteries should last at least a month.
I guess it could be implemented with discrete components, but it'd be hard to keep leakage currents low enough (microamps!) to keep it running for weeks on a coin cell.
I had an ATTINY13A and an ATTINY85 microcontroller available. I started with the 13A but bricked it halfway, so continued with the ATTINY85. The program size is small enough to fit also in the 1KB of the ATTINY13. To keep power consumption low, it is woken up from sleep once per second, checks the sensor and either sounds an alarm or goes back to sleep. According to the datasheet, it consumes 4 microamps in power-down mode with the watchdog timer enabled. A 2032 coin cell has a capacity of 200mAh, enough to last 50,000h or 6 years in sleep mode. Of course the measurements and eventual alarms will reduce this in practice.
The sensor is somewhat non-trivial too: in principle just two copper conductors, whose resistance is infinite when dry, but get a finite resistance when wet. However, if these are placed inside your child's pyjamas, it should be both sturdy and have no sharp edges. I glued some copper filaments between two pieces of cloth and protected the solder bonds with heat-shrink tube.
Step 1: Materials
- An ATTINY85 microcontroller (ATTINY13A should work too) - DIP8 format
- A programmer for the ATTINY (I used an Arduino Uno)
- A small speaker - I used a 27mm speaker from a broken headphone
- A 2- or 3-lead flexible cable to connect the sensor to the unit (I used the cord from the same broken headphone)
- A small prototype PCB - 3x6 cm will fit. I used the stronger 'green' double-sided PCB instead of the cheaper 'brown' single-sided PCB - it seems MUCH stronger.
- An 8-pin DIP-8 chip socket - or use 2 4-pin pieces of female pin-header
- An on/off switch
- A 1 microfarad capacitor
- A 3.5mm headphone jack - optional: it allows to disconnect the sensor from the main unit
- A box - for me the whole project fitted inside a small tic-tac box!
The sensor is made from a piece a cloth folded double with 2 or three strands of copper inside. When it gets wet is will conduct. If it’s resistance becomes less than that of the internal pull-up resistor (~50kOhm) of the ATTINY, the alarm will go off.
For the copper strands I used very thin uninsulated copper, simply taken from a multi-strand cable: those used for making connections on breadboard have ~20 strands.
To connect it to the main unit, I used a headphone cable, which allows to unplug the sensor, rather handy to let it dry. These cables have three strands, so I used all three, putting the one connected to the ground in the middle. But two strands would be fine too. Heat-shrink tube is then used to cover the sharp edges of the soldering. With a few spots of glue the copper strands are held in place and the cloth is folded double.
Step 2: The Electronics
Adjust the size of the prototype board to fit inside the box, together with all components.
Solder all components on the prototype board, and apply all the connections between the components.
The connections are not many, they are listed here:
- The minus of the battery goes to pin 4 of the ATTINY and to one of the leads of the sensor
- The plus of the battery goes to pin 8 of the ATTINY, but is interrupted by an on/off switch
- The two other leads of the sensor go to pin 2 and 3 of the ATTINY
- The speaker is connected to pin 5 and 6, but a 1muF capacitor is placed in series to limit the current
Note the absence of external resistors: a voltage divider is made by using the internal pull-up resistors of the ATTINY.
Step 3: The Box
I challenged myself to fit the electronics inside a small tic-tac box. I had to resort to using a tiny on-off switch to achieve that and also cut out part of the bottom.
The speaker is very fragile so it’s good to have it inside the box. Holes in the box will help the sound coming out. Two more holes in the lid are needed for the 3.5mm jack and the switch
Step 4: Programming the ATTINY
Programming an ATTINY85 or ATTINY13 is not as trivial as programming an Aduino, but it is quite doable.
I followed the following steps:
- Starting up the Arduino IDE, open 'Preferences' then add https://raw.githubusercontent.com/damellis/attiny/ide-1.6.x-boards-manager/package_damellis_attiny_index.json for additional board managers. Under 'Tools/Board', select 'Board Manager' and at the bottom of the list, install 'attiny by David A. Mellis'
- Connect the Arduino Uno and upload 'ArduinoISP'. It is available under 'File/examples'
- 3. Connect the Arduino to the ATTINY, easiest done by putting the ATTINY on a prototype shield: PIN xx of the Arduino to PIN xx of the ATTINY
- Choose 'Board Attiny24/45/85', 'Processor ATtiny85', 'Clock Internal 1 MHz' and programmer 'Arduino as ISP' (attention: not 'ArduinoISP' but 'Arduino as ISP' )
- Upload the attached BWalarm.ino
If there are no error messages, probably all went well, and the ATTINY can be moved to the alarm.
The alarm is meant to give a short beep when switched on, and the full alarm can be checked by short-circuiting the pins corresponding to the sensor for at least 1 second, to give a three-tone alarm.
Some comments about the program:
- The short beep inside setup is very useful to check the battery and make sure the device is switched on.
- The watchdog timer is set up wake up every second. This helps tremendously in saving battery.
- The interrupt routine is defined but nothing is done inside. The program just continues the loop after waking up from sleep_mode.
- Sound is made by bit-banging pin 5 and 6 (PB0 and PB1). There was no need to save CPU and use timers for sound. By inverting the polarity between the two pins, the amplitude of the beep is maximal and a reasonably loud sound is obtained even with just a coin cell.
- The beep function is very basic, and really meant to minimise the program size.
- The sensor is read out twice, and an alarm is given if either goes low. Using a two-strand sensor, one of the analogRead() functions could be removed.
- The threshold value of 512 is meant to be halfway 0 and 1023. It can be tuned if needed. The difference between dry and wet however is large enough that dry means reading out 1023 and wet much less than 100.
Step 5: Using the Device
See the video at the start-page!