digital inputs and digital outputs over I2C

Hi all,

I had this crazy notion that just a bunch of digital inputs and a bunch of digital outputs would be relatively easy. The hardware part, yeah, easy, I've been doing that stuff for decades.

I knew the code would be a challenge. I have done ladder logic and CNC G-code programming off and on for years but never any "C".

My project:

I bought a pretty old scissor lift, it didn't run and needed electrical work. I got that all done, but I want to make some functional improvements and simplify the wiring up to the platform from the base.

The functionality:

The electrical system is basic small engine and 12VDC.

In the platform, there are nine functions (inputs) all N/O and mostly momentary contacts. These include:

Forward
Reverse
2nd speed
3rd speed
right turn
left turn
platform up
platform down
engine choke

There are two more inputs coming from the base:
platform full down position
platform below 10 feet

The outputs:
forward (travel locked out when platform is above 10')
reverse (travel locked out when platform is above 10')
2nd speed (energized when platform is less than 10' up and joystick is at least half travel, forward or reverse.
3rd speed (energized when platform full down AND joystick is full forward or full reverse)
right turn
left turn
brake release (energized when travel only)
hydraulic pressure unloader (energized with any function)

The hardware:

16 relay board from ebay (http://www.ebay.com/itm/16-Channel-12V-Relay-Module-Optocoupler-Protection-Power-Supply-Arduino-PIC-DSP-/232106661468?hash=item360aa2965c:g:p~4AAOSwLnBX97eb

I2C interface for above (http://www.iascaled.com/store/Hobbyist/I2C-RELAY16)

arduino micro (Arduino Micro — Arduino Official Store)

DC to DC converter (http://www.ebay.com/itm/DC-DC-5V-40V-To-1-2-36V-Buck-Converter-Adjustable-Step-Down-Power-Module-200W-8A-/192068809279?hash=item2cb831723f:g:8OMAAOSwj2dXkIGN)

Status:

Im running Win7 64 bit.
downloaded arduino IDE: OK
downloaded Relay16 library: FAIL maybe. I have a lib folder Relay16 with Relay16.cpp and relay16.h files
at C:\program files(x86)\Arduino\libraries\Relay16
downloaded relay 16 example: FAIL invalid library
connected to arduino micro and configure IDE: OK
hardware breadboarded: ok (yes I know there are no pull down resistors on there yet)
no inputs on arduino configured yet.

Relay16 example will verify ok but fails to upload and fails with invalid library.

pics of project

This sounds odd:

Relay16 example will verify ok but fails to upload and fails with invalid library.

If there is a problem with a library that a sketch is using, the verification fails.
Failure to upload a sketch could have a number of causes.

You are using an arduino pro micro which has I2C pins 2&3 (not 4&5 as found on the Uno and others) and it is also just possible the library you are using does not support the 32U4 chip of the micro.

Start by posting the sketch you are having trouble with together with the error messages and a link to the relay driver library the sketch is using.



Are you sure an i2c bus is going to work with that length of cable? It's not really designed to work over distances more than a few hundred mm. You may need some extra line drivers or at least some stronger pull-ups.

You don't need pull-down resistors on the pushbuttons. Just use INPUT_PULLUP and let the switches pull the pins to ground.

@6V6GT (I remember those!)

It could well be that the lib isn’t compatible. I find it really hard to find info on little details like that. That would explain why the lib is invalid only when trying to upload it.

The sketch:

/*************************************************************************
Title:    Iowa Scaled Engineering I2C-RELAY16 Driver Library Example Sketch
Authors:  Nathan D. Holmes <maverick@drgw.net>
File:     $Id: $
License:  GNU General Public License v3

LICENSE:
   Copyright (C) 2014 Nathan D. Holmes & Michael D. Petersen

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   For more information about the Iowa Scaled Engineering I2C-RELAY16, see:
   http://www.iascaled.com/store/I2C-RELAY16

*************************************************************************/

#include <Wire.h>
#include <Relay16.h>

Relay16 relayBoard;

void setup() {
 // initialize serial communications at 9600 bps:
 Serial.begin(9600);
 Wire.begin();
//  Both of the following initialization examples assume that address jumpers J5, J6, and J7
//  on the I2C-RELAY16 are set to low (center pin is jumpered to the one away from the + sign on
//  each jumper block).  If they're set the other direction (center to + pin), replace "LOW" with "HIGH"

//  If you're using a standard Iowa Scaled shield to connect the I2C lines to the Arduino,
//  the /IORST line is likely tied to Digital IO line 4.  Use the initializer below.
 relayBoard.begin(LOW, LOW, LOW, 4);

//  If the /IORST line on the I2C cable isn't connected anywhere, use this initializer
//   instead of the one above.
//  relayBoard.begin(LOW, LOW, LOW, -1);

}

void loop() {
 // print the results to the serial monitor:

 byte relayNum;

 for (relayNum = 1; relayNum <= 16; relayNum++)
 {
   relayBoard.relayOn(relayNum);
   delay(250);
   relayBoard.relayOff(relayNum);  
   
 }

 relayBoard.allOn();
 delay(250);
 relayBoard.allOff();
 delay(250);   

}

The lib/example

http://www.iascaled.com/info/Relay16LibraryReference

Thanks for the reply``

@PaulRB

Where do you find the specs on I2C ? I couldn’t find anything.

I know serial wire runs can be picky. Lots of experience with RS232 in noisy environments.

I was planning on getting the circuit to work, then trying the proper cable length.

I debated using the INPUT_PULLUP but there is a safety issue. Generally on mobile equipment you don’t want a pull down condition for initiating some function. The reason being, if a wire rubs through and gets grounded a function could unexpectedly start. In essence, you can’t accidentally create a positive signal with a failure to ground. It’s probably overkill on this thing, but the last thing you want is for the machine to start moving when up in the air. Those things are shakey enough as is :slight_smile:

Thanks for your input

jeff238:
Generally on mobile equipment you don’t want a pull down condition for initiating some function. The reason being, if a wire rubs through and gets grounded a function could unexpectedly start

On a copter, wouldn’t it be just as much of a problem if a function failed to operate? That’s what happens if you have a pull down that gets grounded.

Re: i2c length. That's what I've been warned about by more experienced people, and I suppose I am parroting those warnings. Personally, I have used i2c at around 1.5m using the internal pull-ups on esp8266 (or at least Wemos) with no problems.

Please read the forum guidelines about posting code, and edit your post above to correct it.

code tags applied, thanks.

I sent an email to the board manufacturer about max i2c length. Also asked about compatibility with the micro. We will see what they say.

So, in 6V6GT's post he thought my micro may not be compatible with the lib supplied. After thinking about it, isn't that what choosing the correct arduino model in the IDE is for? Then the compiler would make the necessary conversion from C++ to the machine code for that particular chip.

Am I missing something?

I can always change the arduino model. I just chose the micro for the # of I/O and didn't need all the extras that the other models have.

Can you some how print the error message you are getting when you upload the sketch.
If you can't get as far as uploading, then that problem has to be solved before progressing further.

To the point you made about selecting the correct "Arduino" in the IDE, it is correct that things like pin number mappings etc. between Arduinos using he various processor types ( 32U4, 328P etc.) should be taken care of. However, if the developer has used features which are not available on a particular chip (timerX etc. etc.) then there will be a problem.

You should also at some stage produce a schematic (even a photo of a hand drawn diagram) which shows which pin numbers on the various devices are interconnected etc.

I seriously doubt that the choice of Pro Micro is the problem.

I just downloaded the i2c-Relay16 library. I could not get the "add library from .zip" to work. No errors, I just could not see/select the .zip, even though it is there. I am just starting to use the "add library" feature, and have had it work OK previously with other libraries.

So then I tried to install the library manually, in the way I am more familiar with, extracting the contents of the .zip to /home/paul/sketchbook/libraries (I use Ubuntu). I restarted the IDE and copied your sketch above and attempted to compile ("verify") it. Failed, could not find the library. So I tried renaming the folder from "i2c-Relay16-master" to "i2c_Relay16". No joy.

Exploring the files unzipped, there seems to be one or two more layers of subfolders than is usual. I went to the /home/paul/sketchbook/libraries/i2c_relay16/src/Relay16 folder, and moved the "Examples" folder, plus the Relay16.cpp and Relay16.h files up a couple of levels to /home/paul/sketchbook/libraries/i2c_relay16.

Then I tried again to compile/verify your sketch. All good.

I did not attempt to upload to an Arduino, but as 6v6gt already said, a problem with a library causes the compile to fail, not the upload. So follow my steps and see if that sorts it for you.

PaulRP,

You traced my steps for installing the lib almost exactly. That part I finally got right after looking at some other libs that were pre installed.

I did refer to the end result in my OP, but not how I got there.

So, yes I finally stripped out all the crap and just left the Relay16.cpp and the Relay16.h files in a folder Relay16 in the libraries folder.

I will copy the same folder to the appropriate sketch folder and see if that helps. I’m thinking not, because the IDE creates that folder from the include function. If it’s just a link or a copy of the files, I don’t know…

I did email the vendor that published the lib and bitched that it didn’t work. In the mean time I figured it out. Their answer was, no the zip method doesn’t work, so do this and this and this and it will work. The shame is that if they packaged the lib properly it would just work.

On the subject of i2c wire length.

I found this:

Everything you would want to know about the i2c spec.

The wire length is only limited by wire capacitance. For the standard speed spec that is 400pf max. The wire i will use is 300pf per 100 meters. I will be using maybe 11 meters due to the zigzag path up the scissor. Well within the spec.

It is also interesting to note that the digital low and digital high are referenced from the input voltage (max 7.7v). None the less, I will put a scope on the end and see how much signal I have left.

That is if I ever get this thing coded…

I'm moving on to the programming section, since it seems the hardware side of this is feasible.

Thanks for all the help guys.

Another option for libraries is to put the files in the same folder as the project then open them up in project tabs.

"I'm moving on to the programming section, since it seems the hardware side of this is feasible."

The mods will want all posts on the same project in the same thread. Otherwise when you go over the code part you will get asked hardware questions already answered in this one. If you get told "don't cross post, threads merged" then that's what happened.

GoForSmoke:
Another option for libraries is to put the files in the same folder as the project then open them up in project tabs.
. . .

This is particularly useful if you modify a “standard” library.
Remember of course the slightly different format of the include statement:

#include <somethingFromStandardLibraryLocation.h>
#include "somethingFromSketchFolder.h"

6v6gt:
This is particularly useful if you modify a "standard" library.
Remember of course the slightly different format of the include statement:

It's the best way that the IDE lets me write my own. Saves only go to the project folder which when the #include points to the/a library folder your edits don't get loaded on compile.

But that's how I know it works with no wondering what goes where. And... if the library works there it can work and live with the rest of the libraries in folder-land.