2

I have a large amount of sensor boards that I need to be able to configure setting unique addresses. Each sensor is attached to it's own arduino, I'm using a 8 position DIP switch for setting the address.

I have tried diferent approaches. When I switch On and OFF I get random values. I understood that I would be able to get up to 255 possible positions, but without being able to get the proper byte data I would have to manually code each possibility, which seems wrong.

Schematic :

enter image description here

Code

#include "DipSwitchI2C.h"

DipSwitchI2C::DipSwitchI2C(int address)
{
    _address = address;
}

void DipSwitchI2C::begin()
{
    Wire.begin();
}

int DipSwitchI2C::read()
{
    unsigned int address = 0;
    unsigned int value = 0;

    Wire.requestFrom(_address, 1);
    if (Wire.available())
    {
        value = Wire.read();
    }
    Wire.endTransmission();

    for (int i = 0; i < 8; i++)
    {
        address += (value & (1<<i)) > 0;
    }

    return address;
}
Marc
  • 131
  • 6
  • 1
    A 8 position DIP switch can have 256 positions. And you are just calculating the sum of bits. – gre_gor Aug 20 '18 at 21:38
  • First of all, you must be sure that the _address has the right i2c address of the pcf8574, then you can do a Wire.requestFrom(_address,1) and a return(Wire.read()). That's all, two lines, nothing else. – Jot Aug 20 '18 at 22:39
  • it is unclear what this means .... without being able to get the proper byte data – jsotola Aug 20 '18 at 23:48
  • since you have only one sensor board on any given Arduino, why do you need a unique address on the sensor board? – jsotola Aug 20 '18 at 23:50
  • 1
    determine what is the value of Wire.read() at this line value = Wire.read(); .... you may be surprised – jsotola Aug 21 '18 at 00:25
  • @jsotola I have network of sensors connected by rs485. I need to be able to ask the sensor reading for every arduino from a master – Marc Aug 21 '18 at 06:17

2 Answers2

1

As @jsotola noted the solution was simple:

int DipSwitchI2C::read()
{
    unsigned int address = 1;

    Wire.requestFrom(_address, 1);
    if (Wire.available())
    {
        address = Wire.read();
    }
    Wire.endTransmission();

    return address;
 }
Marc
  • 131
  • 6
0

Assuming that your switches are on pins 2-9 on an UNO. Change pins to match what you actually have.

byte addressVal = 0;
for(int i = 2: i<10; i++){
    addressVal <<= 1;
    addressVal += digitalRead(i);
}

This will check each switch and add a 1 to the end of the byte. On each iteration it shifts the contents of that byte 1 place to the left. After it reads all the pins you have a byte with a value 0 to 255 depending on the states of the pins.

Delta_G
  • 3,270
  • 2
  • 10
  • 24
  • 1
    The switches are connected to the arduino by a PCF8574 multiplexer not directly to the pins. – Marc Aug 21 '18 at 06:18
  • Well then replace the digitalRead command with whatever command you use to read a pin from the multiplexer. – Delta_G Aug 21 '18 at 23:19