Introduction: 8 Relays With 1 PIO
There are only 3 PIOs available in the MicroBit without buying the adapter.
The code makes it possible to remotely control a large number of relays using just 1 PIO !
The program uses a cut down version of my joystick keyboard instructable.
Step 1: How to Use It
Use the joystick to select the required relay by moving the control from left to right.
Moving the control down switches the associated relay on & moving the control up switches the selected relay off.
To indicate the relay state, when it is on, the number in the matrix is bright and dim when the relay is off.
The receiver display mimics the transmitter display and controls the relay.
Step 2: How It Works
The program uses a neopixel display and an LDR (photoresistor) to control the relays.
The LDR forms part of a voltage divider with a 7.5K ohm resistor.
See here to learn more about a voltage divider :- https://ohmslawcalculator.com/voltage-divider-calculator
To turn these particular relays on, the control voltage must be low (a zero).
Each LDR and neopixel led pair are isolated by a 3D printed housing. I only had white filament in my printer so I painted it black (with nail varnish actually) so light from an LED would not affect an adjacent LED.
When the LED is off, its resistance is high so the value at the voltage divider is high too!
When the led is on, the LDR resistance is low which pulls the divider voltage low thus turning the relay on!
So, in effect, I have created an addressable opto isolator !!
Step 3: The Software
There are two parts to the software, the transmitter & the receiver !
The transmitter selects relays 1 to 8 using a joystick. Left / right increments and decrements the counter controlled by an array called RL
RL=["0","1","2","3","4","5","6","7","8","9"]
The relay status is stored in array RLs
RLs=[0,0,0,0,0,0,0,0,0,0]
On / off is controlled by up (off) and down (on).
Example of data to be transmitted - eg control relay 5
A two digit code is transmitted. This consists of the relay number multiplied by 10, with 1 added if the relay is to be switched on.
Relay 5 on data would be 5 x 10 +1 =51
Relay 5 off data would be 5 x 10 = 50
Because arrays always start at 0, the status of array 5 is stored at position 4, so the status is always the relay number -1
The status (RLs) is used to determine the brightness of the relay number in the 5x5 led array.
If the status is 1 (relay on) the number is displayed with high brightness, if its 0 (relay off) then the display is dimmed.
The receiver decodes the data received thus :-
rxrelay = int(rxdata / 10)
rxstat = rxdata % 10
So, again, using relay 5 as an example, data received will be either 50 or 51
The relay to be controlled is the integer 50 / 10 = 5 or 51 /10 =5
and its status is the modulus of 50 % 10 =0 or 51%10=1
As with arrays, neopixel leds start at 0, so to control neopixel 5, 1 must be deducted.
So the receiver knows which neopixel to control and whether to set it to on or off !
The 5x5 led matrix is updated accordingly showing the status of the last relay controlled, dim if off and bright if on!
Step 4: Improvements
The next step would be confirm the receiver has actually done as instructed, so a confirmation sent back to the transmitter would be a bonus !