Skip to content

Commit 753c990

Browse files
author
Jim Lindblom
committed
Updating to version 2.0 - more user-friendly
- Made most functions more user friendly. - digitalWrite, pinMode, and other standard Arduino function calls are used. - Should remain mostly backwards compatible. - blink and breathe functions now try to estimate millisecond durations - Improved clock control
1 parent 9dc4c14 commit 753c990

File tree

20 files changed

+1677
-836
lines changed

20 files changed

+1677
-836
lines changed

examples/analogWrite/analogWrite.ino

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*************************************************************
2+
analogWrite.ino
3+
SparkFun SX1509 I/O Expander Example: pwm output (analogWrite)
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 analogWrite function.
9+
Connect an LED to the SX1509's pin 15 (or any other pin, they
10+
can all PWM!). The SX1509 can either sink or source current,
11+
just don't forget your limiting resistor!
12+
13+
Hardware Hookup:
14+
SX1509 Breakout ------ Arduino -------- Breadboard
15+
GND -------------- GND
16+
3V3 -------------- 3.3V
17+
SDA ------------ SDA (A4)
18+
SCL ------------ SCL (A5)
19+
15 -------------------------------- LED+
20+
LED- -/\/\/\- GND
21+
330
22+
23+
Development environment specifics:
24+
IDE: Arduino 1.6.5
25+
Hardware Platform: Arduino Uno
26+
SX1509 Breakout Version: v2.0
27+
28+
This code is beerware; if you see me (or any other SparkFun
29+
employee) at the local, and you've found our code helpful,
30+
please buy us a round!
31+
32+
Distributed as-is; no warranty is given.
33+
*************************************************************/
34+
35+
#include <Wire.h> // Include the I2C library (required)
36+
#include <SparkFunSX1509.h> // Include SX1509 library
37+
38+
// SX1509 I2C address (set by ADDR1 and ADDR0 (00 by default):
39+
const byte SX1509_ADDRESS = 0x3E; // SX1509 I2C address
40+
SX1509 io; // Create an SX1509 object to be used throughout
41+
42+
// SX1509 Pin definition:
43+
const byte SX1509_LED_PIN = 15; // LED to SX1509's pin 15
44+
45+
void setup()
46+
{
47+
// Call io.begin(<address>) to initialize the SX1509. If it
48+
// successfully communicates, it'll return 1.
49+
if (!io.begin(SX1509_ADDRESS))
50+
{
51+
while (1) ; // If we fail to communicate, loop forever.
52+
}
53+
54+
// Use the pinMode(<pin>, <mode>) function to set our led
55+
// pin as an ANALOG_OUTPUT, which is required for PWM output
56+
io.pinMode(SX1509_LED_PIN, ANALOG_OUTPUT);
57+
}
58+
59+
void loop()
60+
{
61+
// Ramp brightness up, from 0-255, delay 2ms in between
62+
// analogWrite's
63+
for (int brightness=0; brightness<256; brightness++)
64+
{
65+
// Call io.analogWrite(<pin>, <0-255>) to configure the
66+
// PWM duty cycle
67+
io.analogWrite(SX1509_LED_PIN, brightness);
68+
delay(2); // Delay 2 milliseconds
69+
}
70+
delay(500); // Delay half-a-second
71+
72+
// Ramp brightness down, from 255-0, delay 2ms in between
73+
// analogWrite's
74+
for (int brightness=255; brightness>=0; brightness--)
75+
{
76+
io.analogWrite(SX1509_LED_PIN, brightness);
77+
delay(2); // Delay 2 milliseconds
78+
}
79+
delay(500); // Delay half-a-second
80+
81+
}

examples/blink/blink.ino

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*************************************************************
2+
blink.ino
3+
SparkFun SX1509 I/O Expander Example: blink output
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 set-it-and-forget-it
9+
blink function. We'll set the pin up as an OUTPUT, and call
10+
io.blink() all in setup(), then watch the LED blink by itself
11+
in loop().
12+
13+
Hardware Hookup:
14+
SX1509 Breakout ------ Arduino -------- Breadboard
15+
GND -------------- GND
16+
3V3 -------------- 3.3V
17+
SDA ------------ SDA (A4)
18+
SCL ------------ SCL (A5)
19+
15 -------------------------------- LED+
20+
LED- -/\/\/\- GND
21+
330
22+
23+
Development environment specifics:
24+
IDE: Arduino 1.6.5
25+
Hardware Platform: Arduino Uno
26+
SX1509 Breakout Version: v2.0
27+
28+
This code is beerware; if you see me (or any other SparkFun
29+
employee) at the local, and you've found our code helpful,
30+
please buy us a round!
31+
32+
Distributed as-is; no warranty is given.
33+
*************************************************************/
34+
35+
#include <Wire.h> // Include the I2C library (required)
36+
#include <SparkFunSX1509.h> // Include SX1509 library
37+
38+
// SX1509 I2C address (set by ADDR1 and ADDR0 (00 by default):
39+
const byte SX1509_ADDRESS = 0x3E; // SX1509 I2C address
40+
SX1509 io; // Create an SX1509 object to be used throughout
41+
42+
// SX1509 Pin definition:
43+
const byte SX1509_LED_PIN = 15; // LED to SX1509's pin 15
44+
45+
void setup()
46+
{
47+
// Call io.begin(<address>) to initialize the SX1509. If it
48+
// successfully communicates, it'll return 1.
49+
if (!io.begin(SX1509_ADDRESS))
50+
{
51+
while (1) ; // If we fail to communicate, loop forever.
52+
}
53+
54+
// Set up the SX1509's clock to use the internal 2MHz
55+
// oscillator. The second parameter divides the oscillator
56+
// clock to generate a slower LED clock. 4 divides the 2MHz
57+
// clock by 2 ^ (4-1) (8, ie. 250kHz). The divider parameter
58+
// can be anywhere between 1-7.
59+
io.clock(INTERNAL_CLOCK_2MHZ, 4);
60+
61+
io.pinMode(SX1509_LED_PIN, OUTPUT); // Set LED pin to OUTPUT
62+
63+
// Blink the LED pin -- ~1000 ms LOW, ~500 ms HIGH:
64+
io.blink(SX1509_LED_PIN, 1000, 500);
65+
// The timing parameters are in milliseconds, but they
66+
// aren't 100% exact. The library will estimate to try to
67+
// get them as close as possible. Play with the clock
68+
// divider to maybe get more accurate timing.
69+
}
70+
71+
void loop()
72+
{
73+
// Relax! The SX1509's got this...
74+
}

examples/breathe/breathe.ino

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*************************************************************
2+
breathe.ino
3+
SparkFun SX1509 I/O Expander Example: breathe output
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 set-it-and-forget-it
9+
breathe function. The SX1509 will pulse an LED, smoothly
10+
ramping its brightness up-then-down. We'll set the pin up as
11+
an ANALOG_OUTPUT, and call io.breathe() all in setup(), then
12+
watch the LED pulse by itself in loop().
13+
14+
Hardware Hookup:
15+
SX1509 Breakout ------ Arduino -------- Breadboard
16+
GND -------------- GND
17+
3V3 -------------- 3.3V
18+
SDA ------------ SDA (A4)
19+
SCL ------------ SCL (A5)
20+
15 --------------------------------- LED+
21+
LED- -/\/\/\- GND
22+
330
23+
24+
Development environment specifics:
25+
IDE: Arduino 1.6.5
26+
Hardware Platform: Arduino Uno
27+
SX1509 Breakout Version: v2.0
28+
29+
This code is beerware; if you see me (or any other SparkFun
30+
employee) at the local, and you've found our code helpful,
31+
please buy us a round!
32+
33+
Distributed as-is; no warranty is given.
34+
*************************************************************/
35+
36+
#include <Wire.h> // Include the I2C library (required)
37+
#include <SparkFunSX1509.h> // Include SX1509 library
38+
39+
// SX1509 I2C address (set by ADDR1 and ADDR0 (00 by default):
40+
const byte SX1509_ADDRESS = 0x3E; // SX1509 I2C address
41+
SX1509 io; // Create an SX1509 object to be used throughout
42+
43+
// SX1509 Pin definition:
44+
const byte SX1509_LED_PIN = 15; // LED to SX1509's pin 15
45+
46+
void setup()
47+
{
48+
// Call io.begin(<address>) to initialize the SX1509. If it
49+
// successfully communicates, it'll return 1.
50+
if (!io.begin(SX1509_ADDRESS))
51+
{
52+
while (1) ; // If we fail to communicate, loop forever.
53+
}
54+
55+
// Use the internal 2MHz oscillator.
56+
// Set LED clock to 500kHz (2MHz / (2^(3-1)):
57+
io.clock(INTERNAL_CLOCK_2MHZ, 3);
58+
59+
// To breathe an LED, make sure you set it as an
60+
// ANALOG_OUTPUT, so we can PWM the pin:
61+
io.pinMode(SX1509_LED_PIN, ANALOG_OUTPUT);
62+
63+
// Breathe an LED: 1000ms LOW, 500ms HIGH,
64+
// 500ms to rise from low to high
65+
// 250ms to fall from high to low
66+
io.breathe(SX1509_LED_PIN, 1000, 500, 500, 250);
67+
// The timing parameters are in milliseconds, but they
68+
// aren't 100% exact. The library will estimate to try to
69+
// get them as close as possible. Play with the clock
70+
// divider to maybe get more accurate timing.
71+
}
72+
73+
void loop()
74+
{
75+
// Enjoy your hypnotically breathing LED!
76+
}

examples/clock/clock.ino

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*************************************************************
2+
clock.ino
3+
SparkFun SX1509 I/O Expander Example: clock output
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 clock output
9+
functionality. The OSC pins (OSCIO in the datasheet), can be
10+
configured as either a clock input, clock output, or an
11+
extra output!
12+
13+
Hardware Hookup:
14+
SX1509 Breakout ------ Arduino
15+
GND -------------- GND
16+
3V3 -------------- 3.3V
17+
SDA ------------ SDA (A4)
18+
SCL ------------ SCL (A5)
19+
OSC ------------- Check with a multimeter or o-scope
20+
21+
Development environment specifics:
22+
IDE: Arduino 1.6.5
23+
Hardware Platform: Arduino Uno
24+
SX1509 Breakout Version: v2.0
25+
26+
This code is beerware; if you see me (or any other SparkFun
27+
employee) at the local, and you've found our code helpful,
28+
please buy us a round!
29+
30+
Distributed as-is; no warranty is given.
31+
*************************************************************/
32+
33+
#include <Wire.h> // Include the I2C library (required)
34+
#include <SparkFunSX1509.h> // Include SX1509 library
35+
36+
// SX1509 I2C address (set by ADDR1 and ADDR0 (00 by default):
37+
const byte SX1509_ADDRESS = 0x3E; // SX1509 I2C address
38+
SX1509 io; // Create an SX1509 object to be used throughout
39+
40+
void setup()
41+
{
42+
// Call io.begin(<address>) to initialize the SX1509. If it
43+
// successfully communicates, it'll return 1.
44+
if (!io.begin(SX1509_ADDRESS))
45+
{
46+
while (1) ; // If we fail to communicate, loop forever.
47+
}
48+
49+
// Configure clock:
50+
// - INTERNAL_CLOCK_2MHZ: Set clock to internal 2MHz
51+
// - 2: Set LED clock to divide by 2^(2-1) (2)
52+
// - OUTPUT: Configure OSCIO pin as a clock OUTPUT
53+
// - outputFreq: Sets the frequncy of output
54+
// - 0: 0Hz LOW
55+
// - 0x1-0xE: fOSCout = Fosc / 2 ^ (outputFreq - 1) Hz
56+
// - 0xF: 0Hz HIGH
57+
byte outputFreq = 6; // Set output freq. to 62.5 kHz
58+
io.clock(INTERNAL_CLOCK_2MHZ, 2, OUTPUT, outputFreq);
59+
}
60+
61+
void loop()
62+
{
63+
}

0 commit comments

Comments
 (0)