Skip to content

Commit 7570b67

Browse files
committed
Create DigitalReadWriteCombined.ino
Add updated example code and serial output from Jim's tutorial based on v3 of the library. The example combines the digitalRead() and digitalWrite().
1 parent 10c4479 commit 7570b67

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*************************************************************
2+
digitalReadWriteCombined.ino
3+
SparkFun SX1509 I/O Expander Example: digital I/O (digitalRead/digitalWrite)
4+
Jim Lindblom @ SparkFun Electronics
5+
Original Creation Date: September 21, 2015
6+
https://github.com/sparkfun/SparkFun_SX1509_Arduino_Library
7+
8+
This example demonstrates the SX1509's digitalRead and digitalWrite
9+
functionality. We'll attach an active-low button to an
10+
INPUT_PULLUP input and attach an LED to a pin set as OUTPUT.
11+
Then whenever the button read's LOW, we'll toggle the LED.
12+
Note that the code will wait until the button is released
13+
before reading the SX1509 pins again.
14+
15+
After uploading the sketch, open your serial monitor and set
16+
it to 115200 baud.
17+
18+
Hardware Hookup:
19+
SX1509 Breakout ------ Arduino -------- Breadboard
20+
GND -------------- GND
21+
3V3 -------------- 3.3V
22+
SDA ------------ SDA (A4)
23+
SCL ------------ SCL (A5)
24+
0 ---------------------------------]BTN[----GND
25+
15 -------------------------------- LED+
26+
LED- -/\/\/\- GND
27+
330
28+
Development environment specifics:
29+
IDE: Arduino 1.6.5
30+
Hardware Platform: Arduino Uno
31+
SX1509 Breakout Version: v2.0
32+
33+
This code is beerware; if you see me (or any other SparkFun
34+
employee) at the local, and you've found our code helpful,
35+
please buy us a round!
36+
37+
Distributed as-is; no warranty is given.
38+
*************************************************************/
39+
40+
#include <Wire.h> // Include the I2C library (required)
41+
#include <SparkFunSX1509.h> //Click here for the library: http://librarymanager/All#SparkFun_SX1509
42+
43+
// SX1509 I2C address (set by ADDR1 and ADDR0 (00 by default):
44+
const byte SX1509_ADDRESS = 0x3E; // SX1509 I2C address
45+
SX1509 io; // Create an SX1509 object to be used throughout
46+
47+
// SX1509 pin definitions:
48+
// Note: these aren't Arduino pins. They're the SX1509 I/O:
49+
const byte SX1509_LED_PIN = 15; // LED connected to 15 (source ing current)
50+
const byte SX1509_BUTTON_PIN = 0; // Button connected to 0 (Active-low button)
51+
52+
bool ledState = false;
53+
54+
void setup()
55+
{
56+
// Serial is used in this example to display the input value
57+
// of the SX1509_INPUT_PIN input:
58+
Serial.begin(115200);
59+
Serial.println("SX1509 Example");
60+
61+
Wire.begin(); //Initialize I2C bus
62+
63+
pinMode(13, OUTPUT); // Use pin 13 LED as debug output
64+
digitalWrite(13, LOW); // Start it as low
65+
66+
67+
// Call io.begin(<address>) to initialize the SX1509. If it
68+
// successfully communicates, it'll return 1.
69+
if (io.begin(SX1509_ADDRESS) == false)
70+
{
71+
Serial.println("Failed to communicate. Check wiring and address of SX1509.");
72+
digitalWrite(13, HIGH); // If we failed to communicate, turn the pin 13 LED on
73+
while (1)
74+
; // If we fail to communicate, loop forever.
75+
}
76+
77+
// Call io.pinMode(<pin>, <mode>) to set any SX1509 pin as
78+
// either an INPUT, OUTPUT, INPUT_PULLUP, or ANALOG_OUTPUT
79+
// Set output for LED:
80+
io.pinMode(SX1509_LED_PIN, OUTPUT);
81+
// Use a pull-up resistor on the button's input pin. When
82+
// the button is pressed, the pin will be read as LOW:
83+
io.pinMode(SX1509_BUTTON_PIN, INPUT_PULLUP);
84+
85+
// Blink the LED a few times before we start:
86+
for (int i = 0; i < 5; i++)
87+
{
88+
// Use io.digitalWrite(<pin>, <LOW | HIGH>) to set an
89+
// SX1509 pin either HIGH or LOW:
90+
io.digitalWrite(SX1509_LED_PIN, HIGH);
91+
delay(100);
92+
io.digitalWrite(SX1509_LED_PIN, LOW);
93+
delay(100);
94+
}
95+
}
96+
97+
void loop()
98+
{
99+
// Use io.digitalRead() to check if an SX1509 input I/O is
100+
// either LOW or HIGH.
101+
if (io.digitalRead(SX1509_BUTTON_PIN) == LOW)
102+
{
103+
// Print the status of the other pin:
104+
Serial.print("SX1509_BUTTON_PIN status: ");
105+
// Read the pin to print either 0 or 1
106+
Serial.println(io.digitalRead(SX1509_BUTTON_PIN));
107+
108+
// If the button is pressed toggle the LED:
109+
ledState = !ledState;
110+
io.digitalWrite(SX1509_LED_PIN, ledState);
111+
112+
// Print the status of the other pin:
113+
Serial.print("SX1509_LED_PIN status: ");
114+
// Read the pin to print either 0 or 1
115+
Serial.println(ledState);
116+
117+
118+
Serial.print("Waiting for button to release...");
119+
while (io.digitalRead(SX1509_BUTTON_PIN) == LOW)
120+
; // Wait for button to release
121+
Serial.println("Button released!");
122+
123+
//delay(200); //uncomment to add a small delay for button debouncing
124+
}
125+
}

0 commit comments

Comments
 (0)