Control relay with gpio

I am trying to control a SainSmart relay using a python3 script on radxa5. My code:

#!/usr/bin/python3

import OPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
channel = 36
GPIO.setup(channel, GPIO.OUT)
GPIO.output(channel, GPIO.HIGH)

if GPIO.input(channel) == 1:
relay_state = ‘relay off’

if GPIO.input(channel) == 0:
relay_state = ‘relay on’

print(relay_state)

time.sleep(10)
GPIO.output(channel, GPIO.LOW)
if GPIO.input(channel) == 1:
relay_state = ‘now relay off’

if GPIO.input(channel) == 0:
relay_state = ‘now relay on’

print(relay_state)

time.sleep(10)
GPIO.cleanup()

I also tried pins 16 and 18.
The code prints out “relay off” and “now relay on”, but when I test the relay with an ohm meter none of the contacts change at all, no matter which pair I test.
I have a bunch of raspberrys operating relays and they all work just fine.
This is confusing and I don’t have a clue what to do. It seems this should be simple. I run this script in Thonny and get no errors. Googled: radxa5 relay and got total of 9 hits and only one was radxa info, several were raspberry info.
Any gpio gurus around here?

Truth is taht people buy rock 5 just to use its high power cpu. No makers anymore :wink: Last time I used GPIO was on Nano pi neo and wiring pi python libraries. Radxa does not provide any software support for its product. I think you should go with OrangePi as they provide support for makers.

1 Like

Does this mean that what I want to do is just not possible? Seems that when gpio is advertised they need to respond, gee if not I will start building sbc advertised it with all kinds of stuff and simply not deliver. Something sure seems wrong with this whole idea.

Hey what do you employees of Radxa have to say about this?

Szymon, thanks for your comments. I am using the gpio to turn on led lights on my 3d-printer, how do you do it?

Hi @ed100
Can you please post a link to the relais module that you are using and also the OS you are running? It might be possible that you are using a module that expects 5V input levels to switch (instead of 3?3v) and/or more current than the GPIOs of the 5B can provide.
Hab you measured the output pins of the 5B and verified that the logic level changes as expected?

Brand: SAINSMART
2-Channel 5V Relay Module

Debian 11 from the radxa download page

I have the relay connected to 4V, negative and 36 (but also tried 16 and 18).

According to the python script when it runs, it prints:
relay on
now relay off

When I measure across the relay with an ohm meter, there is no change in the state of the relay. The relay is new, just out of the package and works on a raspberry that I used to test the relay.
I do not have any wiring on the output side of the relay, so the current requirement of the relay should be very slight. The radxa5 can surely supply that.

I have the relay connected to 5V, not 4v–can’t type very well

Hi @ed100
Thanks for the update! I was more worried about the drive strength of the GPIO output than the 5V supply of the rock 5B :slight_smile:
Have you tried to directly connect the multimeter between GND and the GPIO header of the 5B an measure the output voltage? It should switch between 0V and 3.3V depending on what you set in your python script. In case there is something mixed up between the GPIO numbers in SW and HW it can also help to let your python script loop over a big range of channels in case you do not immediately see changes in the output voltage as expected.

get voltage there, will work on this more on Fri, have to be out of town tomorrow

Hello,

I was testing the GPIO capabilities and found below reference.
https://wiki.radxa.com/Rock5/hardware/5b/gpio

I was trying to light an LED over GPIO via the cli method mentioned in the above link. It worked for pin 16 but failed for pin 8. Can you verify this?

Also, regarding your relay, try connecting v+ of relay to 3.3v on the board rather than 5v, see if that works…

I changed to 3.3 volts instead of 5V and with Debian 11 no joy. I booted Armbian and was able to get relay to work. I plan to continue working on the Debial 11 version. I read some on Raspberry sites about relay and one of them mentioned that to run at 5V it needed a resistor so that may have been why it ran ok on 3.3v. In any event running Armbian and 3.3v makes the relay operate with my script. I did change to using pin 36. It also ran on pin 16 but could not get it to run on pin 8.

2 Likes

I am currently testing and attempting to use the GPIO along with several libraries.

So far, I have installed the radxa+official MRAA fork. It works on the CLI, not in python.
The adafruit package I cannot seem to get to work.

Using the CLI information from radxa, I have found like above I can control pin 16. However, some I cannot seem to. Pins 8,10,12 are all stuck at 3.3v no matter whether I set it high or low. The same goes for pins 3, 5, 33, & 40. Pin 22 seems to give me 1.8?