Introduction: DTMF Controlled Gate
In this world where everything is controlled through Android and IOS apps, I wanted to control the gate to our premises with ordinary DTMF (Dual Tone Multi Frequency) codes.
So I ordered an MT8870 module, and a PCF8574 port expander. An Attiny85 I had still lying around. The Attiny85 will control a relay that operates the gate when the secret telephone code is keyed in at the handset.
The system will be in our landline connection, so no loose mobile phone hanging around waiting for someone to call it. The system should be able to learn the secret code (preferably with an asterisk and a hash sign in it). And it should be able to remember this code after power outage. The EEPROM in the Attiny is well suited for that.
The Attiny85 can put itself to sleep, such as to minimize the power consumption. An interrupt will wake it and it will continue processing.
The Attiny85 has only one limitation: it has only 5 digital I/O lines. The MT8870 already needs 5, and then we still have the learn switch and the relay to connect.
This is where the PCF8574 comes in. It provides for 8 I/O lines and can talk I2C to the Attiny.
Step 1: Connect All the Components
Q1-Q4 of the MT8870 connect to P0-P3 of the PCF8574. This is the binary representation of the detected key on the handset.
The STD line of the MT8870 connects to the INT3 of the Attiny. It would be possible to connect the STD to the PCF8574 and use interrupt line of the PCF8574. The PCF8574 starts interrupting as soon as any input changes, and the STD is only asserted when Q1-Q4 are fully latched. This would mean that the Attiny would have to check the STD line and this could cause unnecessary power consumption and possible hang-situations for the CPU.
A0-A2 of the PCF8574 connect to ground. This results in device address 0x20 of the PCF8574.
SDA and SCL (the I2C bus) of the PCF8574 connect to SDA and SCL of the Attiny. Both lines need a pull-up resistor.
P4 of the PCF8574 connects to the 'learn'-switch that, when asserted, pulls this input to ground.
PB4 of the Attiny operates the relay for half a second, when the keyed-in code sequence matches the learned code-sequence.
All components need 5v power supply. The jack-input of the MT8870 connects to the phoneline.
Step 2: The Attiny Sketch
In the sketch I use the following features of the Attiny85:
- the sleep function
- pin change interrupts
- EEPROM read and write
- I2C through the TinyWire library
I spent a lot of time finding that the PB3/INT3 line (where STD of the MT8870 connects to) needs the internal pull-up resistor.
The sketch works as follows: After setting everything up in preparation (reading the EEPROM, setting up interrupts and ports and setting up TinyWire), the infinite while loop runs. In this loop, the first thing the CPU does is goto sleep.
After a pin change interrupt, the CPU wakes and it is determined whether this was a rising edge interrupt. If so, the PCF8574 is read out through I2C. This returns the code of the phone key, plus the 'learn' bit.
If the system is called to learn, the keys are recorded until there are four. These are then stored in the EEPROM.
If the system is called to play, the keys are checked against the learned code. If four keys in a row are matched, PIN4 is set to 'high' for half a second and then 'low' again.
Before relooping the while-loop, the sketch waits until STD is low again (ready for the next key).
Step 3: Putting It All Together
The sketch is compiled with the Atmel Studio and the resulting hex file is put on a Raspberry Pi
The Pi is connected to the Attiny, the hex file is programmed from the Raspberry Pi into the Attiny with avdrude (see other Instructable article on how to do this).
A jack-jack cable connects the MT8870 with a laptop
A tone generator on the Internet will generate key codes.