In this example we are going to wire up a relay board and let your Xojo code control it.
A really board is normally used to control high voltage devices such as lights for example. In my case I will be using them to remote control electric heaters in a summer house in the winters when I am not there.
Be smart when doing projects with relays. If you have very large relay boards operating many relays at once then it will draw a lot of current from your Raspberry PI and the Raspberry PI does not allow you to draw unlimited amount of it.
Max output for any single GPIO is 16 ma with total current from all GPIO pins at around 50 ma.
The relay I used draws around 4 ma from the GPIO pin when turning it to HIGH per relay. And it was draws around 14,5 ma from from 5V power pin. So powering all four of them would be taking around 16 ma from the GPIO’s total budget and around 58ma from the 5V pin.
(How much your 5V pin can supply depends a bit on your power supply)
We do not take any responsibility for possible errors in the guide or errors that you might do wiring it up. Incorrect wiring can result in damaged relay board or damaged Raspberry PI. Connecting the high voltage connectors can potentially be life threatening, be careful and do at your own risk.
Pieces we use are:
- Raspberry PI 2
- Cobbler and Cobbler cable
- 4 way relay board. (I bought 5 pieces of 4 way boards for $14 from store called A+A+A+ on Aliexpress)
- Wiring PI Xojo module from Paul Lefebvre
First thing is connecting the relay board:
- (5V) Red is the 5V input, its marked as VCC on the board – I always use red for the power input.
- (GND) Blue is the ground. I always use blue for ground
- (GPIOX1) Yellow is connected to IN1 which means yellow will control relay 1
- (GPIOX2) Green is connected to IN3 which means yellow will control relay 3
To connect your high voltage device such as light bulb, then you use the ends of the relays as shown above.
If you are just testing then you can skip connecting high voltage device. Sounds from the relays and lights on it will show you if your experiment worked.
You will not be connecting your high voltage ground to any of the connectors.
If your connecting a bulb for example to relay one on the board then
- High voltage ground goes to the Bulb
- High voltage input goes to the middle connector on the relay.
- High voltage output that goes from the relay to the bull will go either in the left or the right connector on the relay depending on if you want the bulb on or off when the relay is powered.
Connecting the cobbler:
Make sure your Raspberry PI is not powered on when you do the connections.
In my example then I have it connected as follows:
- Red to pin 2 (5V)
- Blue to pin 6 (GND)
- Yellow to GPIO 4 which is pin 7.
- Green to GPIO 17 which is pin 11.
The Xojo code:
I had a window, with two buttons on it, one says Toggle Relay 1 the other says Toggle Relay 3.
In the window I added the following properties:
Protected Relay1On As Boolean = false Protected Relay2On As Boolean = false
The Window Open event was as follows:
Const kRelay1Pin = 4 Const kRelay2Pin = 17 GPIO.SetupGPIO GPIO.PinMode(kRelay1Pin, GPIO.OUTPUT) GPIO.PinMode(kRelay2Pin, GPIO.OUTPUT) // I want both the relays to be off when I power on: GPIO.DigitalWrite(kRelay1Pin, GPIO.OFF) GPIO.DigitalWrite(kRelay2Pin, GPIO.OFF)
Then the button events where as follows:
Sub Action() Const kRelay1Pin = 4 if Relay1On then GPIO.DigitalWrite(kRelay1Pin, GPIO.OFF) Relay1On = false else GPIO.DigitalWrite(kRelay1Pin, GPIO.ON) Relay1On = true end if End Sub
Sub Action() Const kRelay2Pin = 17 if Relay1On then GPIO.DigitalWrite(kRelay2Pin, GPIO.OFF) Relay2On = false else GPIO.DigitalWrite(kRelay2Pin, GPIO.ON) Relay2On = true end if End Sub
Thats all there is to it!
AliExpress.com Product - 1pcs/lot 4 channel relay module 4-channel relay control board with optocoupler. Relay Output 4 way relay module for arduino