Created
October 31, 2024 19:46
-
-
Save bboyho/b16ec679b9aac2baa8f0f0b30499bff3 to your computer and use it in GitHub Desktop.
Combined Qwiic Power Switch and I2C Scanner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Modified Example of Using the Qwiic Power Switch | |
Modified by: Ho Yun "Bobby" Chan | |
By: Paul Clark (PaulZC) | |
Date: April 23rd, 2020 | |
Based extensively on: | |
Using the PCA9536 -- Digital Output | |
By: Jim Lindblom | |
SparkFun Electronics | |
Date: May 4, 2018 | |
License: This code is public domain but you buy me a beer | |
if you use this and we meet someday (Beerware license). | |
This example demonstrates how to use the Qwiic Power Switch | |
and read the additional GPIO pins. The Arduino I2C scanner | |
[ https://playground.arduino.cc/Main/I2cScanner/ ] is | |
also included to detect which I2C device(s) have been isolated. | |
Hardware Connections: | |
Attach your Arduino to the Qwiic Power Switch IN | |
Plug your Qwiic device into the Qwiic Power Switch OUT | |
https://www.sparkfun.com/products/15081 | |
*/ | |
#include <Wire.h> | |
#include <SparkFun_Qwiic_Power_Switch_Arduino_Library.h> // Click here to get the library: http://librarymanager/All#SparkFun_Qwiic_Power_Switch | |
QWIIC_POWER mySwitch; | |
void setup(void) { | |
Serial.begin(115200); | |
while (!Serial) | |
; //Wait for user to open terminal | |
Serial.println(F("Qwiic Power Switch Example")); | |
Wire.begin(); | |
if (mySwitch.begin() == false) //Connect to the power switch using Wire port | |
{ | |
Serial.println(F("Qwiic Power Switch not detected at default I2C address. Please check wiring. Freezing.")); | |
while (1) | |
; | |
} | |
// Configure GPIO1 and GPIO2 as INPUT | |
mySwitch.pinMode(1, INPUT); | |
mySwitch.pinMode(2, INPUT); | |
Serial.println(F("1) Enable power and I2C")); | |
Serial.println(F("2) Disable power and I2C")); | |
Serial.println(F("3) Enable I2C isolation")); | |
Serial.println(F("4) Disable I2C isolation")); | |
Serial.println(F("5) I2C Scanner")); | |
} | |
void loop() { | |
if (Serial.available()) { | |
byte incoming = Serial.read(); | |
if (incoming == '1') { | |
// Switch the power on | |
mySwitch.powerOn(); | |
Serial.println(F("Power is ON. I2C isolation is disabled.")); | |
} else if (incoming == '2') { | |
// Switch the power off | |
mySwitch.powerOff(); | |
Serial.println(F("Power is OFF. I2C isolation is enabled.")); | |
} else if (incoming == '3') { | |
// Enable I2C isolation = I2C bus _is_ isolated | |
mySwitch.isolationOn(); | |
Serial.println(F("I2C isolation enabled. I2C is isolated.")); | |
} else if (incoming == '4') { | |
// Disable I2C isolation = I2C bus _is not_ isolated | |
mySwitch.isolationOff(); | |
Serial.println(F("I2C isolation disabled. I2C is not isolated.")); | |
} else if (incoming == '5') { | |
// Scan I2C bus | |
Serial.println(F("I2C scanner.")); | |
i2cScanner(); | |
} | |
// Read and print the GPIO1/GPIO2 state | |
Serial.print(F("GPIO1 is: ")); | |
Serial.println(mySwitch.digitalRead(1)); | |
Serial.print(F("GPIO2 is: ")); | |
Serial.println(mySwitch.digitalRead(2)); | |
// Read any extra Serial bytes (e.g. CR or LF) | |
while (Serial.available() > 0) { | |
Serial.read(); | |
} | |
} | |
} | |
void i2cScanner() { | |
byte error, address; | |
int nDevices; | |
Serial.println("Scanning..."); | |
nDevices = 0; | |
for (address = 1; address < 127; address++) { | |
// The i2c_scanner uses the return value of | |
// the Write.endTransmisstion to see if | |
// a device did acknowledge to the address. | |
Wire.beginTransmission(address); | |
error = Wire.endTransmission(); | |
if (error == 0) { | |
Serial.print("I2C device found at address 0x"); | |
if (address < 16) | |
Serial.print("0"); | |
Serial.print(address, HEX); | |
Serial.println(" !"); | |
nDevices++; | |
} else if (error == 4) { | |
Serial.print("Unknown error at address 0x"); | |
if (address < 16) | |
Serial.print("0"); | |
Serial.println(address, HEX); | |
} | |
} | |
if (nDevices == 0) | |
Serial.println("No I2C devices found\n"); | |
else | |
Serial.println("done\n"); | |
delay(5000); // wait 5 seconds for next scan | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment