About Us Knowledge Base Community Forum Electronics Toolbox FAQ Shipping & Delivery

Community Forum

Share your projects and post your questions

Search
Register or Sign In
Forum

Example C code for IO Pi Plus needed

61 Views - Created 7 days ago

7 days ago

Posted by:
el Ducko

Last edited: 09/02/2020

Users Avatar

Location:
Texas, USA

Having just bought and stacked a couple of IO Pi Plus cards, I'm ready to hook 'em into my model railroad program and drive some relays. I'm rooting around, trying to find some C code that will show me how to do that. I've found a nice explanation in multiple buttons lighting multiple leds but it's written in python and has code such as

from ABE_helpers import ABEHelpers
from ABE_IoPi import IoPi

i2c_helper = ABEHelpers()
i2c_bus = i2c_helper.get_smbus()

# create the two buses
bus1 = IoPi(i2c_bus, 0x20)
bus2 = IoPi(i2c_bus, 0x21)

# set bus 1 to be inputs
bus1.set_port_direction(0, 0xFF)
bus1.set_port_direction(1, 0xFF)

#....etc

which I can't use in C. I haven't had a look at the ABE_helpers file(s) to see if I can "borrow" some code to translate into C. A much better solution would be to find a similar example in C code which can point me in the direction that I need to go.

Can anyone help with a nice, clear example, please? I imagine that it will involve downloading  from abelectronicsuk / ABElectronics_C_Libraries

Computer geek since 1964. Upgraded to FORTRAN66 back in the day. Downhill ever since.

7 days ago

Posted by:
andrew

Users Avatar

Location:
United Kingdom

Users Twitter  Users Website  

We have a C library for the IO Pi which includes some demos for writing and reading to the IO Pi bus.  You can find them on our github repository at https://github.com/abelectronicsuk/ABElectronics_C_Libraries/tree/master/IOPi

One thing to be aware of when driving relays with the IO Pi Plus is each chip can only supply 20mA per channel and a total of 125mA per bus so you will probably need to use a transistor or darlington array to drive the relay coils. 

We have a design for a 16 channel relay board that works with the IO Pi Plus which we have made available for anyone to download and build themselves, you can find it at https://www.abelectronics.co.uk/kb/article/1060/relay-board-for-the-io-pi-plus-2-1  We do not sell the relay board ourselves at the moment but you can use the supplied gerber files order PCBs from a supplier like iTead or JLC PCB.

AB Electronics UK Tech Support

7 days ago

Posted by:
el Ducko

Users Avatar

Location:
Texas, USA

Perfect, Andrew- -  exactly what I needed. Thanks for the speedy response, especially on a Sunday. ...time to "rattle some iron," as we used to say in the petrochemical business. 

...wish I had known about that relay board when I speced out my hardware. I wound up stacking 8-relay boards and fumbling with building custom 20-wire ribbon cables from the IO Pi Plus cards to connect to 1x10 pin setups on the relay cards. I used pairs of 2x20 socket plugs, plugging the extra row on each with a hot glue gun. Necessity is the mother of... Well, we won't go there. Thanks for the great help. I'll report back, hopefully soon.

Computer geek since 1964. Upgraded to FORTRAN66 back in the day. Downhill ever since.

6 days ago

Posted by:
el Ducko

Users Avatar

Location:
Texas, USA

So far, so good. I downloaded your  https://github.com/abelectronicsuk/ABElectronics_C_Libraries/tree/master/IOPi  material, copied test.c from the IOPi directory,  "apt-get" got and installed i2c-tools and libi2c-tools, and edited the top of my test.c copy to

#include 
#include 
#include "home/pi/ABElectronics_C_Libraries/IOPi/ABE_IoPi.h"
#include "home/pi/ABElectronics_C_Libraries/I2CSwitch/ABE_I2CSwitch.h"
#include 

I modified my$PATH to look in /home/pi/ABElectronics_C_Libraries/IOPi and /home/pi/ABElectronics_C_Libraries/I2CSwitch. I tried building the code with

gcc -Wall -o "%e" "%f" -lwiringPi

(I'll need the -lwiringPi switch for my other code.) Unfortunately, I got error messages from the machine trying to link to what may be a library. Message reads: "/tmp/cc7m8eMe.o: In function 'main': undefined reference to IOPi_init"... ditto to "set_port_direction" and the other lines of code. (The usleep() function was no problem. ...makes sense. It's looking specifically for I2C stuff.)

I've probably left out something or installed something wrong. ...any ideas? I think I'm missing links to a library of some sort, but am not sure.

Computer geek since 1964. Upgraded to FORTRAN66 back in the day. Downhill ever since.

6 days ago

Posted by:
andrew

Users Avatar

Location:
United Kingdom

Users Twitter  Users Website  

You may need to include the ABE_IoPi.c in the gcc command.

The command I normally use to compile the demo and test programs is

gcc ABE_IoPi.c test.c -o test

It is often easiest to copy the ABE_IoPi.c, ABE_IoPi.h, ABE_I2CSwitch.c and ABE_I2CSwitch.h into the same directory as your program so you can reference them without needing the full path name.

Another alternative is to create a make file that will include all of the libraries and automatically link everything together. 

AB Electronics UK Tech Support

6 days ago

Posted by:
el Ducko

Last edited: 10/02/2020

Users Avatar

Location:
Texas, USA

That worked! Your test.c program compiled, and seems to run okay. However, it failed

IOPi_init(0x20)

and other address number attempts with

Failed to write to i2c device for write

so I'm now wondering if I've fouled up the hardware during soldering..(I shouldn't have used an old high-powered soldering gun from the discrete component days.)

The IO Pi Plus 2.1 cards get 5 volts okay, externally supplied (jumper un-soldered).. On one card, the address is set to 0x20; that is, solder pads 0/1/2 are not soldered together for either bus 1 or bus 2. (One thing that I see wrong is that bus 2 pads 0 should be soldered together, right?) Due to poor soldering technique on my part, I've fried a couple of the address solder pads, then tried to remove the solder on the remaining ones, and may have fried something in the process. On my second card, I managed to destroy only one of the solder pads, and have connected the "1" pads to set it up for address 22. Looks like I can solder Bus 2 pads 0 together again, so maybe this card can be salvaged too.

Post image

So, what are your thoughts? Have I destroyed the cards and will have to buy two more? Will soldering pads 0 together on both cards' bus 2 fix my problem? (...just tried it. Nope- -  no joy.) ...any other thoughts?

Computer geek since 1964. Upgraded to FORTRAN66 back in the day. Downhill ever since.

6 days ago

Posted by:
andrew

Users Avatar

Location:
United Kingdom

Users Twitter  Users Website  

Looking at the photos your IO Pi boards should still be usable.

On the first board if you resolder pad 0 on bus 1 and make sure there is no connection between pads 1 on bus 1 that should give you I2C addresses of 0x20 and 0x21. 

It looks like you have broken the 5V pcb trace that connects all of the top address pins together so you will need to solder a wire between the top pad 1 on bus 1 and the solder bridge on pad 0 on bus 2.  Without this there will be no 5V supply to the I2C level translator and the board will not appear on the bus.  I have marked on the image below with a purple line where the wire has to go.

On the second board the 5V trace looks intact so if you leave bus 1 with pad 1 soldered and solder pad 0 on bus 2 that should give you I2C addresses of 0x22 and 0x23.

Post image

You can check if the boards are appearing on the I2C bus by using the command:

sudo i2cdetect -y 1

The i2cdetect command scans the I2C bus and returns a list of addresses that have devices.

If you currently only have a high power soldering iron then it may be worth investing in a smaller iron, or ideally a soldering station with a variable temperature control. 

We use the Metcal SP200 soldering stations which we found on ebay for around £50.  They have replaceable tips that come in a wide range of sizes and temperatures and can be used for soldering devices from large connectors down to 0201 surface mount components.

AB Electronics UK Tech Support

5 days ago

Posted by:
el Ducko

Users Avatar

Location:
Texas, USA

Thanks for the great help, Andrew. I made the changes you suggested, ran i2cdetect, and still got nothing.

It's time for a confession. As I did the first "smoke test" and powered up the RasPi with the two boards, I wasn't sure how the software worked, but rambled through your AB Electronics website, blundered into i2cdetect, and tried it with no results. I had no idea if i2cdetect told me anything useful, so continued pursuing other software.

I then started checking my wiring, and found that the voltage was 1.5 volts instead of the expected 5 volts. I suspected that the power supply socket I had soldered to one of the boards was installed wrong, removed it, and wired it to the far end of my power bus. This again gave the wrong voltage. I started working my way down the power bus, and discovered to my horror that the wires to the IO Pi Plus boards were reversed. I swapped them and finally got the correct 5 volt power on the power bus. I crossed my fingers, hoping that there was no lasting damage, and started chasing the software angle through this series of posts.

I suspect that I have fried both boards, Am I correct? In that case, I'll need to buy two more boards. I have re-wired the power bus with color-coded wire. (The boards aren't repairable, right?) I worry that I might also have damaged all the other boards, but I can't tell until I get the IO Pi Plus boards running.

So, I've learned a couple of things: (1) use my new low-power soldering gun, and be more careful, especially on the fragile address pads (2) Color-code not just the power bus wiring but all wiring, and double check it with a voltmeter berore connecting it to any of the boards, especially the IO Pi Plus boards (3) don't mount the external power supply socket on either IO Pi Plus board (4) get the software running and check out the IO Pi Plus boards before wiring up the other boards. (5) wire up the boards one-at-a-time, checking as you go.

Thanks for sticking with me through this series of blunders. (Spoiler alert: it's not over yet!)

Computer geek since 1964. Upgraded to FORTRAN66 back in the day. Downhill ever since.

5 days ago

Posted by:
andrew

Users Avatar

Location:
United Kingdom

Users Twitter  Users Website  

If you wired the power up backwards then I am afraid that you have probably damaged the IO chips and possibly the mosfet that controls the I2C level shifting.  The only way to repair the boards would be to replace the MCP23017 IO chips but that is difficult to do without a hot air rework station.

AB Electronics UK Tech Support

5 days ago

Posted by:
el Ducko

Users Avatar

Location:
Texas, USA

Looks like I'll have to replace them, then. I'll get two new boards on order and, when they come in, carefully install them. Thanks for all your help, Andrew, and for setting me upon the right path.

Computer geek since 1964. Upgraded to FORTRAN66 back in the day. Downhill ever since.

Note: documents in Portable Document Format (PDF) require Adobe Acrobat Reader 5.0 or higher to view, download Adobe Acrobat Reader or other PDF reading software for your computer or mobile device.