|
1 | 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 |
| 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 | 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! |
| 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 | 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 |
| 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 | 21 | 330
|
22 | 22 |
|
23 |
| -Development environment specifics: |
24 |
| - IDE: Arduino 1.6.5 |
25 |
| - Hardware Platform: Arduino Uno |
26 |
| - SX1509 Breakout Version: v2.0 |
| 23 | + Development environment specifics: |
| 24 | + IDE: Arduino 1.6.5 |
| 25 | + Hardware Platform: Arduino Uno |
| 26 | + SX1509 Breakout Version: v2.0 |
27 | 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! |
| 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 | 31 |
|
32 |
| -Distributed as-is; no warranty is given. |
| 32 | + Distributed as-is; no warranty is given. |
33 | 33 | *************************************************************/
|
34 | 34 |
|
35 |
| -#include <Wire.h> // Include the I2C library (required) |
36 |
| -#include <SparkFunSX1509.h> // Include SX1509 library |
| 35 | +#include <Wire.h> // Include the I2C library (required) |
| 36 | +#include <SparkFunSX1509.h> //Click here for the library: http://librarymanager/All#SparkFun_SX1509 |
37 | 37 |
|
38 | 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 |
| 39 | +const byte SX1509_ADDRESS = 0x3E; // SX1509 I2C address |
| 40 | +SX1509 io; // Create an SX1509 object to be used throughout |
41 | 41 |
|
42 | 42 | // SX1509 Pin definition:
|
43 | 43 | const byte SX1509_LED_PIN = 15; // LED to SX1509's pin 15
|
44 | 44 |
|
45 |
| -void setup() |
| 45 | +void setup() |
46 | 46 | {
|
47 |
| - // Call io.begin(<address>) to initialize the SX1509. If it |
| 47 | + Serial.begin(115200); |
| 48 | + Serial.println("SX1509 Example"); |
| 49 | + |
| 50 | + Wire.begin(); |
| 51 | + |
| 52 | + // Call io.begin(<address>) to initialize the SX1509. If it |
48 | 53 | // successfully communicates, it'll return 1.
|
49 |
| - if (!io.begin(SX1509_ADDRESS)) |
| 54 | + if (io.begin(SX1509_ADDRESS) == false) |
50 | 55 | {
|
51 |
| - while (1) ; // If we fail to communicate, loop forever. |
| 56 | + Serial.println("Failed to communicate. Check wiring and address of SX1509."); |
| 57 | + while (1) |
| 58 | + ; // If we fail to communicate, loop forever. |
52 | 59 | }
|
53 |
| - |
54 |
| - // Use the pinMode(<pin>, <mode>) function to set our led |
| 60 | + |
| 61 | + // Use the pinMode(<pin>, <mode>) function to set our led |
55 | 62 | // pin as an ANALOG_OUTPUT, which is required for PWM output
|
56 | 63 | io.pinMode(SX1509_LED_PIN, ANALOG_OUTPUT);
|
57 | 64 | }
|
58 | 65 |
|
59 | 66 | void loop()
|
60 | 67 | {
|
61 |
| - // Ramp brightness up, from 0-255, delay 2ms in between |
| 68 | + // Ramp brightness up, from 0-255, delay 2ms in between |
62 | 69 | // analogWrite's
|
63 |
| - for (int brightness=0; brightness<256; brightness++) |
| 70 | + for (int brightness = 0; brightness < 256; brightness++) |
64 | 71 | {
|
65 |
| - // Call io.analogWrite(<pin>, <0-255>) to configure the |
| 72 | + // Call io.analogWrite(<pin>, <0-255>) to configure the |
66 | 73 | // PWM duty cycle
|
67 | 74 | io.analogWrite(SX1509_LED_PIN, brightness);
|
68 | 75 | delay(2); // Delay 2 milliseconds
|
69 | 76 | }
|
70 | 77 | delay(500); // Delay half-a-second
|
71 |
| - |
72 |
| - // Ramp brightness down, from 255-0, delay 2ms in between |
| 78 | + |
| 79 | + // Ramp brightness down, from 255-0, delay 2ms in between |
73 | 80 | // analogWrite's
|
74 |
| - for (int brightness=255; brightness>=0; brightness--) |
| 81 | + for (int brightness = 255; brightness >= 0; brightness--) |
75 | 82 | {
|
76 | 83 | io.analogWrite(SX1509_LED_PIN, brightness);
|
77 | 84 | delay(2); // Delay 2 milliseconds
|
78 | 85 | }
|
79 | 86 | delay(500); // Delay half-a-second
|
80 |
| - |
81 | 87 | }
|
0 commit comments