4 Relay relay Cape 'c' code

Could someone display an example of operating and releasing a relay in ‘c’?
And if possible getting the current state of the relay. Operated or released
.

I have the relay cape from Mouser 958-RLYCPE-BBBCAPE GHI installed.

Relay Cape. From what I read.

Relay 1 gpio20
Relay 2 gpio 7
Relay 3 gpio112
Relay 4 gpio115

If possible code not requiring libraries or header files not normally on the BBB image (RCN 10.3 Debian IoT)
unless I can download any required files from a URL.

Thanks

This is what I found so far (thanks zmatt)
relay 1 - P9.41 - gpio 0.20 (gpio20)
relay 2 - P9.42 - gpio 0.07 (gpio7)
relay 3 - P9.30 - gpio 3.16 (gpio112)
relay 4 - P9.27 - gpio 3.19 (gpio115)

Hello KenUnix,

Seth here. I will need to type one up. I found a book a while back on setting up GPIO pins in C/C++ (more towards C than C++). Anyway…

I think I can assist in this matter.

I think that Adafruit_BBIO and some other libraries might go extinct soon. I am not familiar enough w/ Python to write my own library just yet.

I am not familiar enough w/ C/C++ to type up an entire library yet either, e.g. mostly due to headers in C/C++ and import OS and others in Python.

Seth

P.S. Anyway, the book is Exploring BeagleBone by Molloy. I will get on this task soon. Please bear w/ me on this endeavor.

I suspect this is the Relay board you have:
https://beagleboard.org/capes

Have you looked at?
https://github.com/beagleboard/capes

Mala had recently posted questions regarding this board. Have you seen this:
https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/beagleboard/Zv7LaRV3TKU/WzPzcbrjBwAJ

Also, which Beagle Board are you connecting this to?

Relay,

Jon

Jon,

Yes Mala I.E. Seth have exchanged messages

The ‘c’ I tried refuses to operate relays

–Start code–

#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

int main()
{
int fd = open( “/sys/class/gpio/gpio112/value”, O_RDWR | O_CLOEXEC );
if( fd < 0 ) {
fprintf( stderr, “open: %m\n” );
exit(1);
}

char c = ‘1’;
write( fd, &c, 1 );
close( fd );

return 0;
}

–End Code–

No errors. Just dosen’t do anything

Ken

Your trying too hard…

Turn Relay 2 on:

echo 1 > /sys/class/leds/relay-jp2/brightness

Turn Relay 2 off:

echo 0 > /sys/class/leds/relay-jp2/brightness

Regards,

Robert,

Out of curiosity, does that require having the Relay overlay loaded or is that a default?

https://github.com/beagleboard/bb.org-overlays/blob/master/src/arm/BB-RELAY-4PORT-00A0.dts

Cheers,

Jon

int fd = open( "/sys/class/gpio/gpio112/value", O_RDWR | O_CLOEXEC );

  I note you are using the low-level file descriptor calls, whereas most
code I've seen make use of higher level file pointer calls.

https://stackoverflow.com/questions/34261512/gpio-on-beaglebone-black
{somewhat odd that that example does a SEEK to beginning, for files opened
"w", but does not do a seek within the loop when toggling the GPIO value}

  I'll admit most of my experiments have been using Python (and I do not
envisage Adafruit_BBIO just vanishing some day... not unless it too gets
subsumed into CircuitPython via Blinka library [a lot of the former device
specific libraries have gone that route]).

  No experience with https://github.com/ehayon/BeagleBone-GPIO which
bypasses the sysfs calls for memory-mapping the GPIO system.

https://www.beyondlogic.org/an-introduction-to-chardev-gpio-and-libgpiod-on-the-raspberry-pi/
focuses on the R-Pi, but ...

debian@beaglebone:~$ apt-cache search gpiod
gpiod - Tools for interacting with Linux GPIO character device - binary
gpiod-dbgsym - debug symbols for gpiod
libgpiod-dev - C library for interacting with Linux GPIO device - static
libraries and headers
libgpiod-doc - C library for interacting with Linux GPIO device - library
documentation
libgpiod0 - C library for interacting with Linux GPIO device - shared
libraries
libgpiod0-dbgsym - debug symbols for libgpiod0
libgpiod1 - C library for interacting with Linux GPIO device - shared
libraries
libgpiod1-dbgsym - debug symbols for libgpiod1
libgpiod2 - C library for interacting with Linux GPIO device - shared
libraries
libgpiod2-dbgsym - debug symbols for libgpiod2
python3-libgpiod - Python bindings for libgpiod (Python 3)
python3-libgpiod-dbgsym - debug symbols for python3-libgpiod
debian@beaglebone:~$

... seems to be available on BBB -- no idea what the 0/1/2 variants are...
(unfortunately, the link mentions the C API, but doesn't show an example)

No errors. Just dosen't do anything

  Take off the relay board and just wire in an LED and limit resistor
between the GPIO and ground (or 3.3V depending on whether you want "set" to
be on or off) and see if the LED flickers.

Robert,

You were correct high level is better for now. It’s been a long while since working
with ‘c’. Went the way of a bash script. Works well see below. Even supports --help.

—Code Start–

#!/bin/bash

Hello KenUnix,

Seth here. Do you want me to still work on the C++ code or are you satisfied w/ the shell script you are working on currently?

Seth

P.S. I found my book, ideas, and everything is on chapter six w/ source already done for specific ideas. I would probably need to change some source, too. Let a brother know.

Is this what you are referring to?

http://derekmolloy.ie/beaglebone/beaglebone-gpio-programming-on-arm-embedded-linux/

https://github.com/derekmolloy/exploringBB

Jon

Seth,

Yes I am still interested. brb.

Try out that script. It’s kind of neat. Let me know what you think.

After downloading it to a file chmod 755 filename
It’s actually useful cmd is the name you give the
file. In my case I called it relay

cmd state 115
cmd on 20
cmd off 20

cmd label 7
cmd out 112

cmd --help

I just finished another script that exercises the relays.

See you on the Kiwi flip side…

KenUnix

Jon,

Thanks I will look at them.

Did you see that script I uploaded? Give it a try.

Ken

Ken,

Sorry I do not have a Relay board, but I have done the shell scripting thing before.

Are you using a BB Black?

I am curious as to what Seth/Mala comes up with though.

Cheers,

Jon

There’s also a semi-Arduino-like C library I partially worked on described here:
https://www.element14.com/community/community/designcenter/single-board-computers/next-genbeaglebone/blog/2019/08/15/beaglebone-black-bbb-io-gpio-spi-and-i2c-library-for-c-2019-edition

… that was tested in the latter half of 2019, on BBB and PocketBeagle. Turning a pin on/off in C is like this:

Setup:
#include <iobb.h>

iolib_init(); // initialize the library

iolib_setdir(8, 12, DigitalOut); // set the pin as an output

And then controlling a pin connected to a relay board or whatever:

pin_high(8, 12);

pin_low(8,12);

where the ‘8’ represents the connector P8, and the ‘12’ represents pin 12 on that connector.

It’s not pretty code, it’s quite ropey, but it worked in the limited tests I’ve used it for, and it supports I2C and SPI too. Plus it’s fast, toggle speed is > 2MHz.

I started trying to port it to BB-AI, but have put it on pause for now, so many register differences : (

Jon,

I’ll let Seth know.

Regarding me yes I have a BBB with a relay cape running Debian 10.3 IoT 3-26-2020
And a 4 port usb-2 powered hub and a .Realtek RTL8188EUS USB WiFi adapter.

I always like tp glance at code. You never know
when you will find something interesting. That’s
how it was for me when I first started using Unix
back in 1974.

Ken

Hello jonnymo,

Seth here. The book exploringBB has some nice source for C++ workings w/ it geared to, on Chapter 6, GPIO and other peripherals.

Seth

P.S. See here: https://github.com/derekmolloy/exploringBB/tree/version2/chp06. Although this was from two years ago, I am sure if we work on it, minor improvements or some similar changes might be all that is needed. Who knows?

Hello shabaz,

Seth here. I have been trying to find time to use this library. If it works on the new image, I will do my work to make time to use it.

Seth

P.S. I saw this library a long time ago. I have been staying patient in case there were some other people that were to update it. Are you familiar w/ MRAA and UPM libraries from Eclipse?

Hello,

I am thankful that you updated me yesterday. I am waiting to try out the iobb library from shabaz soon. If you test it, let a brother know how it works out.

Seth

Mala,

Yeah, I have 3 variations of Molloy’s books and I do find them quite nice. His code more up to date than the shabaz code, so I would prefer that.

The other option is the AdaFruit BBIO Python code, which I believe Molloy uses.
https://learn.adafruit.com/setting-up-io-python-library-on-beaglebone-black/installation-on-ubuntu

Regardless, its best to just understand how the underline system is working and how to make calls to the GPIO pins and then go your own route.

Jon

The Derek Molloy code (at least from the link below from Seth) uses Linux system ‘sysfs’ to perform the low-level GPIO, so it is far slower than the method I ended up using (which directly maps and modifies the registers). Despite it not being a microcontroller, I still sometimes want that high speed. For instance, I used my library to make an FPGA programmer with the BBB. It runs fast. Also, I already had a BBB book - didn’t really fancy buying another book, I don’t see the point when I can write a simple-to-use GPIO library and document it as well as a book might.

It depends on what’s wanted. I prefer (naturally, otherwise I would not have written it : ) my method, primarily for the high speed and Arduino-like simplicity. The disadvantages of this method are that it’s not as portable (as evidenced by the time taken to port to BB-AI : ( - due to having to go down to register level, so it is restricted to BBB and PocketBeagle so far. Another disadvantage is that the code is poor, it deserves a re-write from scratch. I’d hoped someone would have written a better library over the years so it would not be needed, but fast-forward five years, in 2019 I still couldn’t find a usable fast library, so (for my purposes) I had to ressurect it and re-test it - because of non-backward-compatible changes in the BBB Linux image that had occurred over the years.

Having said all that, if all that is needed is to control relays, today I’d also consider to do it with Python, because that’s fast enough. No point using a fast library unless you plan to use it for future things that may need the higher speed.
Also, nothing wrong perhaps with doing it in a bash script, it’s just not something I prefer (I’m not super-familiar with shell scripts, so I do the bare minimum in such scripts).

For the past few years I’ve also used the Pi (with the wiringPi C library, which is also fast and Arduino-like). I built my own relay board for it. I wrote code for it in C (usable with Python of course) to be able to control relays from Python or from bash scripts etc.

The only suggestions I have based on that experience, is if you’re going to switch multiple relays on or off simultaneously, then it’s ‘friendlier’ electrically to have a few millisecond pause between each. I ended up incorporating that in my code, i.e. for simultaneous control it would automatically stagger by a few milliseconds multiple relays switching on to make it just a perceived simultaneous time.