Skip to content

Commit e55d876

Browse files
committed
0.1.1 I2C_PWM_generator
1 parent 411104c commit e55d876

File tree

12 files changed

+688
-0
lines changed

12 files changed

+688
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
platforms:
2+
rpipico:
3+
board: rp2040:rp2040:rpipico
4+
package: rp2040:rp2040
5+
gcc:
6+
features:
7+
defines:
8+
- ARDUINO_ARCH_RP2040
9+
warnings:
10+
flags:
11+
12+
packages:
13+
rp2040:rp2040:
14+
url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
15+
16+
compile:
17+
# Choosing to run compilation tests on 2 different Arduino platforms
18+
platforms:
19+
- uno
20+
# - due
21+
# - zero
22+
# - leonardo
23+
# - m4
24+
# - esp32
25+
# - esp8266
26+
# - mega2560
27+
# - rpipico
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# These are supported funding model platforms
2+
3+
github: RobTillaart
4+
custom: "https://www.paypal.me/robtillaart"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
name: Arduino-lint
2+
3+
on: [push, pull_request]
4+
jobs:
5+
lint:
6+
runs-on: ubuntu-latest
7+
timeout-minutes: 5
8+
steps:
9+
- uses: actions/checkout@v4
10+
- uses: arduino/arduino-lint-action@v1
11+
with:
12+
library-manager: update
13+
compliance: strict
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: Arduino CI
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
runTest:
7+
runs-on: ubuntu-latest
8+
timeout-minutes: 20
9+
10+
steps:
11+
- uses: actions/checkout@v4
12+
- uses: ruby/setup-ruby@v1
13+
with:
14+
ruby-version: 2.6
15+
- run: |
16+
gem install arduino_ci
17+
arduino_ci.rb
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: JSON check
2+
3+
on:
4+
push:
5+
paths:
6+
- '**.json'
7+
pull_request:
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
timeout-minutes: 5
13+
steps:
14+
- uses: actions/checkout@v4
15+
- name: json-syntax-check
16+
uses: limitusus/json-syntax-check@v2
17+
with:
18+
pattern: "\\.json$"
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
# Change Log I2C_PWM_generator
3+
4+
All notable changes to this project will be documented in this file.
5+
6+
The format is based on [Keep a Changelog](http://keepachangelog.com/)
7+
and this project adheres to [Semantic Versioning](http://semver.org/).
8+
9+
10+
## [0.1.1] - 2025-05-16
11+
- add LED 13 as heartbeat indicator.
12+
- add watchdog reset (2 seconds)
13+
- add POSValue array, power on start values.
14+
- add currentValue array to prep getPWM()
15+
- add get PWM value - request register 0..5, 1 byte
16+
- add set all - register 0x20
17+
- add set to power on reset - register (command) 0x21
18+
- update readme.md
19+
20+
21+
## [0.1.0] - 2024-11-25
22+
- initial version
23+
24+
25+
// -- END OF FILE --
26+
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
//
2+
// FILE: I2C_PWM_generator.ino
3+
// AUTHOR: Rob Tillaart
4+
// VERSION: 0.1.1
5+
// DATE: 2024-11-25
6+
// PURPOSE: Arduino UNO as 6 channel PWM co-processor.
7+
// URL: https://github.com/RobTillaart/I2C_PWM_generator
8+
// https://github.com/RobTillaart/PCA9553/issues/9
9+
//
10+
11+
#include <avr/wdt.h>
12+
#include <Wire.h>
13+
14+
// adjust if needed
15+
#define I2C_BASE_ADDRESS 0x30
16+
#define I2C_ADDRESS_PIN 4
17+
#define I2C_BASE_CLOCK 100000
18+
19+
20+
const int PWMpins[6] = {3, 5, 6, 9, 10, 11};
21+
// power on start (POS) values can be adapted to your need.
22+
const uint8_t POSValue[6] = {0, 0, 0, 0, 0, 0 };
23+
// cache current value, prep for getPWM()
24+
uint8_t currentValue[6];
25+
26+
const int LEDPIN = 13;
27+
uint32_t lastBlink = 0;
28+
29+
int reg = 0;
30+
31+
32+
void setup()
33+
{
34+
Serial.begin(115200);
35+
Serial.println(__FILE__); // for debugging
36+
37+
// HEARTBEAT PIN
38+
pinMode(13, OUTPUT);
39+
digitalWrite(13, HIGH);
40+
41+
// POWER ON START
42+
for (int i = 0; i < 6; i++)
43+
{
44+
analogWrite(PWMpins[i], POSValue[i]);
45+
currentValue[i] = POSValue[i];
46+
}
47+
48+
// I2C ADDRESS
49+
uint8_t I2Caddress = 0x00;
50+
// get address bit
51+
pinMode(I2C_ADDRESS_PIN, INPUT);
52+
if (digitalRead(I2C_ADDRESS_PIN) == HIGH) I2Caddress += 0x01;
53+
54+
I2Caddress += I2C_BASE_ADDRESS; // 0x30..0x31
55+
56+
// START AS SLAVE
57+
Wire.begin(I2Caddress);
58+
Wire.setClock(I2C_BASE_CLOCK);
59+
60+
// WATCHDOG
61+
wdt_enable(WDTO_2S);
62+
63+
// WAIT FOR COMMANDS
64+
Wire.onRequest(requestEvent);
65+
Wire.onReceive(receiveEvent);
66+
}
67+
68+
69+
void loop()
70+
{
71+
// watchdog
72+
wdt_reset();
73+
74+
// heartbeat
75+
uint32_t now = millis();
76+
if ((now - lastBlink) >= 1000)
77+
{
78+
lastBlink = now;
79+
digitalWrite(13, !digitalRead(13));
80+
}
81+
}
82+
83+
84+
/////////////////////////////////////////////////////
85+
//
86+
// EVENT HANDLERS
87+
//
88+
void receiveEvent(int count)
89+
{
90+
// catch no data call
91+
if (count == 0) return;
92+
int value = 0;
93+
// read the "register"
94+
reg = Wire.read();
95+
switch (reg)
96+
{
97+
// PWM set one
98+
case 0 ... 5:
99+
// value missing?
100+
if (count < 2) return;
101+
value = Wire.read();
102+
currentValue[reg] = value;
103+
analogWrite(PWMpins[reg], value);
104+
break;
105+
106+
// PWM set all (experimental)
107+
case 0x20:
108+
value = 0; // default value
109+
if (count >= 2) value = Wire.read();
110+
for (int i = 0; i < 6; i++)
111+
{
112+
currentValue[reg] = value;
113+
analogWrite(PWMpins[reg], value);
114+
}
115+
break;
116+
117+
// PWM power on start (experimental)
118+
case 0x21:
119+
for (int i = 0; i < 6; i++)
120+
{
121+
analogWrite(PWMpins[i], POSValue[i]);
122+
currentValue[i] = POSValue[i];
123+
}
124+
break;
125+
126+
default: // nothing
127+
break;
128+
}
129+
}
130+
131+
132+
// experimental
133+
void requestEvent()
134+
{
135+
// construct answer for current register.
136+
switch (reg)
137+
{
138+
case 0 ... 5:
139+
Wire.write(currentValue[reg]);
140+
break;
141+
default:
142+
// return nothing.
143+
break;
144+
}
145+
}
146+
147+
148+
// -- END OF FILE --

sketches/I2C_PWM_generator/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024-2025 Rob Tillaart
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

0 commit comments

Comments
 (0)