Skip to content

V1.3.0 #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
/*
This example code demonstrates how to change the address of the Single or
Quad Qwiic Relay to one of your choosing. There is a set range of available
addresses from 0x07 to 0x78, so make sure your chosen address falls within
this range. The next thing to note is that when you change the address you'll
need to call an instance of the Qwiic_Relay class that includes your new
address: "Qwiic_Relay relay(YOUR_NEW_ADDRESS_HERE);" so that the new address
is fed to all the available functions. Finally if for some reason you've
forgotten your new address. No big deal, load up the I2C scanner example code
and find out where your product's address is at.
By: Elias Santistevan
SparkFun Electronics
Date: July 2019

License: This code is public domain but you buy me a beer if you use
this and we meet someday (Beerware license).
Example 6 Change I2C Address

This example code demonstrates how to change the address of the Single or
Quad Qwiic Relay to one of your choosing. There is a set range of available
addresses from 0x07 to 0x78, so make sure your chosen address falls within
this range. The next thing to note is that when you change the address you'll
need to call an instance of the Qwiic_Relay class that includes your new
address: "Qwiic_Relay relay(YOUR_NEW_ADDRESS_HERE);" so that the new address
is fed to all the available functions. Finally if for some reason you've
forgotten your new address. No big deal, load up the I2C scanner example code
and find out where your product's address is at.
By: Elias Santistevan
SparkFun Electronics
Date: July 2019

License: This code is public domain but you buy me a beer if you use
this and we meet someday (Beerware license).
*/

#include <Wire.h>
Expand All @@ -23,33 +25,41 @@
// you want to access the alternate addresses.
#define SINGLE_DEFAULT_ADDR 0x18 // Alternate jumper address 0x19
#define QUAD_DEFAULT_ADDR 0x6D // Alternate jumper address 0x6C

#define NEW_ADDR 0x09

// After changing the address you'll need to apply that address to a new
// instance of the Qwiic_Relay class: "Qwiic_Relay relay(YOUR_NEW_ADDRESS_HERE)".
Qwiic_Relay relay(QUAD_DEFAULT_ADDR); // Change to Single Relay Address if using Quad Relay
Qwiic_Relay relay(QUAD_DEFAULT_ADDR); // Change to Single Relay Address if using single relay

void setup()
{
Wire.begin();
Serial.begin(115200);

// Let's see.....
if(relay.begin())
Serial.println("Ready to flip some switches.");
else
Serial.println("Check connections to Qwiic Relay.");

// There is a not so limited but still limited range of
// addresses that are available to you 0x07 -> 0x78.
if(relay.changeAddress(0x09)) // Put the address you want here.
Serial.println("Address changed successfully.");
else
Serial.println("Address change failed...");

delay(2000);


Wire.begin();

Serial.begin(115200);

if(!relay.begin())
{
Serial.println("Can't communicate with Relay. Check wiring or that you have the correct I2C address.");
while(1)
;
}

Serial.println("Example 6 Changing the relay's I2C address.");

// There is a not so limited but still limited range of
// addresses that are available to you 0x07 -> 0x78.

// If you're using a single relay, indicate it by adding "true" as a second argument.
//if(relay.changeAddress(NEW_ADDR, true)

if(relay.changeAddress(NEW_ADDR)) // Put the address you want here.
Serial.println("Address changed successfully.");
else
Serial.println("Address change failed...");

delay(2000);


}

void loop()
Expand Down
45 changes: 18 additions & 27 deletions src/SparkFun_Qwiic_Relay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*/

#include "SparkFun_Qwiic_Relay.h"
#include <stdint.h>
#include <Arduino.h>

Qwiic_Relay::Qwiic_Relay(uint8_t address)
{
Expand Down Expand Up @@ -66,7 +68,7 @@ uint8_t Qwiic_Relay::getState()
// This function gets the version number of the SparkFun Single Relay.
float Qwiic_Relay::singleRelayVersion()
{
float version = _readVersion(FIRMWARE_VERSION);
float version = _readVersion(SFE_SINGLE_FIRMWARE_VERSION);
return (version);
}

Expand Down Expand Up @@ -160,15 +162,24 @@ uint8_t Qwiic_Relay::getState(uint8_t relay)

// This function changes the I-squared-C address of the Qwiic RFID. The address
// is written to the memory location in EEPROM that determines its address.
bool Qwiic_Relay::changeAddress(uint8_t newAddress)
bool Qwiic_Relay::changeAddress(uint8_t newAddress, bool singleRelay)
{

if (newAddress < 0x07 || newAddress > 0x78) // Range of legal addresses
return false;

_i2cPort->beginTransmission(_address);
_i2cPort->write(ADDRESS_LOCATION);
_i2cPort->write(newAddress);
if(singleRelay)
{
_i2cPort->beginTransmission(_address);
_i2cPort->write(SINGLE_CHANGE_ADDRESS);
_i2cPort->write(newAddress);
}
else
{
_i2cPort->beginTransmission(_address);
_i2cPort->write(QUAD_CHANGE_ADDRESS);
_i2cPort->write(newAddress);
}

if (!_i2cPort->endTransmission())
return true;
Expand All @@ -183,13 +194,9 @@ bool Qwiic_Relay::_writeAddress(uint8_t addressToWrite, uint8_t value)
_i2cPort->write(addressToWrite); // Toggle it on....
_i2cPort->write(value);
if (_i2cPort->endTransmission() != 0)
{
return false; // Transaction failed
} // End communcation.
else
{
return true;
}
}
// This function handles I-squared-C write commands for turning the relays on.
// The quad relay relies on the current state of the relay to determine whether
Expand All @@ -203,9 +210,7 @@ void Qwiic_Relay::_writeCommandOn(uint8_t _command)
{
_status = _readCommand(RELAY_ONE_STATUS);
if (_status == QUAD_RELAY_ON)
{ // Is it on? Then....
return; // Do nothing....
}
else
{ // Off?
_i2cPort->beginTransmission(_address); // Start communication.
Expand All @@ -218,9 +223,7 @@ void Qwiic_Relay::_writeCommandOn(uint8_t _command)
{
_status = _readCommand(RELAY_TWO_STATUS);
if (_status == QUAD_RELAY_ON)
{
return;
}
else
{
_i2cPort->beginTransmission(_address);
Expand All @@ -233,9 +236,7 @@ void Qwiic_Relay::_writeCommandOn(uint8_t _command)
{
_status = _readCommand(RELAY_THREE_STATUS);
if (_status == QUAD_RELAY_ON)
{
return;
}
else
{
_i2cPort->beginTransmission(_address);
Expand All @@ -248,9 +249,7 @@ void Qwiic_Relay::_writeCommandOn(uint8_t _command)
{
_status = _readCommand(RELAY_FOUR_STATUS);
if (_status == QUAD_RELAY_ON)
{
return;
}
else
{
_i2cPort->beginTransmission(_address);
Expand Down Expand Up @@ -289,9 +288,7 @@ void Qwiic_Relay::_writeCommandOff(uint8_t _command)
{
_status = _readCommand(RELAY_ONE_STATUS);
if (_status == QUAD_RELAY_OFF)
{ // Is the board off?
return; // Do nothing...
}
else
{ // Then it must be on...
_i2cPort->beginTransmission(_address); // Start communication.
Expand All @@ -304,9 +301,7 @@ void Qwiic_Relay::_writeCommandOff(uint8_t _command)
{
_status = _readCommand(RELAY_TWO_STATUS);
if (_status == QUAD_RELAY_OFF)
{
return;
}
else
{
_i2cPort->beginTransmission(_address); // Start communication.
Expand All @@ -319,9 +314,7 @@ void Qwiic_Relay::_writeCommandOff(uint8_t _command)
{
_status = _readCommand(RELAY_THREE_STATUS);
if (_status == QUAD_RELAY_OFF)
{
return;
}
else
{
_i2cPort->beginTransmission(_address); // Start communication.
Expand All @@ -334,9 +327,7 @@ void Qwiic_Relay::_writeCommandOff(uint8_t _command)
{
_status = _readCommand(RELAY_FOUR_STATUS);
if (_status == QUAD_RELAY_OFF)
{
return;
}
else
{
_i2cPort->beginTransmission(_address); // Start communication.
Expand All @@ -362,7 +353,7 @@ uint8_t Qwiic_Relay::_readCommand(uint8_t _command)
_i2cPort->write(_command);
_i2cPort->endTransmission();

_i2cPort->requestFrom(_address, 1);
_i2cPort->requestFrom(_address, (uint8_t)1);
uint8_t status = _i2cPort->read();
return (status);
}
Expand All @@ -374,7 +365,7 @@ float Qwiic_Relay::_readVersion(uint8_t _command)
_i2cPort->write(_command);
_i2cPort->endTransmission();

_i2cPort->requestFrom(_address, 2);
_i2cPort->requestFrom(_address, (uint8_t)2);
float _versValue = _i2cPort->read();
_versValue += (float)_i2cPort->read() / 10.0;
return (_versValue);
Expand Down
11 changes: 5 additions & 6 deletions src/SparkFun_Qwiic_Relay.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef _SPARKFUN_QWIIC_RELAY_H_
#define _SPARKFUN_QWIIC_RELAY_H_
#pragma once

#include <Arduino.h>
#include <Wire.h>
Expand Down Expand Up @@ -46,7 +45,7 @@ enum SF_SINGLE_RELAY_COMMANDS

TURN_RELAY_OFF = 0x00,
TURN_RELAY_ON,
FIRMWARE_VERSION = 0x04,
SFE_SINGLE_FIRMWARE_VERSION = 0x04,
MYSTATUS
};

Expand All @@ -67,7 +66,8 @@ enum SF_SINGLE_RELAY_STATUS
#define DUAL_SSR_DEFAULT_ADDRESS 0x0A
#define DUAL_SSR_ALTERNATE_ADDRESS 0x0B

#define ADDRESS_LOCATION 0xC7
#define QUAD_CHANGE_ADDRESS 0xC7
#define SINGLE_CHANGE_ADDRESS 0x03
#define INCORR_PARAM 0xFF

class Qwiic_Relay
Expand Down Expand Up @@ -136,7 +136,7 @@ class Qwiic_Relay

// This function changes the I-squared-C address of the Qwiic RFID. The address
// is written to the memory location in EEPROM that determines its address.
bool changeAddress(uint8_t newAddress);
bool changeAddress(uint8_t newAddress, bool singleRelay = false);

private:
uint8_t _address;
Expand Down Expand Up @@ -169,4 +169,3 @@ class Qwiic_Relay

TwoWire *_i2cPort;
};
#endif