Friday, November 12, 2010


Here's a really useful relay board fitted with four 10A mains relays. I bought cheaply a few years ago thinking that it might be useful. The only problem was that it used a PIC16F886 and a weird serial protocol to control the relays and the rest of the I/O.

How about transforming it into a Arduino Relay Slave - just what the Navitrino central heating controller needs?

A quick check of the PIC16F886 data sheet confirmed that the reset, power and clock oscillator pins of the PIC were nearly all in the same position as on the ATmega328 - as used in the Arduino. Those that weren't, could readily be bodged.

The only changes needed were that pin 19 on this board is grounded - no problem if I make D13 an input, and pin 22 - an output to an unused darlington driver, would have to be made OV to keep the ATmega happy.

The board has a 12V dc input, a 5V regulator and a MAX232 serial chip. In addition to the four relays, there are four further outputs driven by the same ULN2803 octal darlington driver IC, plus four opto-isolated inputs. All inputs and outputs have LED indicators fitted to show their status.

The first job was to trace all the connections and work out what I/O was connected to the relevant pins on the Arduino

Pin 1 Reset
Pin 2 RXD I1.0
Pin 3 TXD I1.1
Pin 4 Digital 2 Relay 0
Pin 5 Digital 3 Input I1.2
Pin 6 Digital 4 Opto-Input I0.3
Pin 7 +5V Input I1.3
Pin 8 0V 0V
Pin 9 OSC1 Osc1
Pin 10 OSC2 Osc2
Pin 11 Digital 5 Relay 3
Pin 12 Digital 6 Opto Input 0.2
Pin 13 Digital 7 OP 1.3
Pin 14 Digital 8 to connector J5
Pin 15 Digital 9 to connector J5
Pin 16 Digital 10 to connector J5
Pin 17 Digital 11 Serial Output (MAX232)
Pin 18 Digital 12 Serial Input (MAX232)
Pin 19 Digital 13 0V
Pin 20 AVcc +5V
Pin 21 Aref
Pin 22 0V OP 1.2
Pin 23 AN0/ Dig 14 OP 1.1
Pin 24 AN1/ Dig 15 Relay 1
Pin 25 AN2/ Dig 16 OP1.0
Pin 26 AN3/ Dig 17 Relay 2
Pin 27 AN4/ Dig 18 Opto-Input 0.0
Pin 28 AN5/ Dig 19 Opto-Input 0.1

Once I was sure that the wiring was OK, I replaced the resonator with a 16MHz crystal and fitted one wire link to correctly feed 5V to pin 7. I then made up a 4 wire cable with a header that would fit my FTDI programming cable. With this all done, I powered up the board without the ATmega fitted and confirmed that my power and ground arrangements were good.

I wrote a short test sketch to set up the I/O with the correct pin functions and to produce a slow sequence of turning the reays on and off. I programmed this into an ATmega on another known working board and then swapped the IC into my relay board - remarkably it all works fine! I have subsequently reprogrammed the ATmega in-situ with the FTDI cable and all is well.

That's the underlying beauty of Arduino, you take a bootloaded ATmega328, add a crystal and connect power and ground and very quickly you have a common I/O platform for rapid prototyping of your latest hardware project.

For a couple of hours work - I now have a working relay slave board, onto which I can develop some code. The board will be used to drive the central heating, hot water and pump functions which require mains relays to operate. A simple seria slave link as developed for my "serial command interpreter" back in August will allow this board to be driven from the main Navitrino master - or possibly remotely via the internet using the Etherduino.

Who said bodging was all bad?

No comments: