Skip to content

Commit 7e064d5

Browse files
committed
update ESP32 LEDC usage
1 parent 89e3823 commit 7e064d5

File tree

4 files changed

+127
-71
lines changed

4 files changed

+127
-71
lines changed

Adafruit_Prop_Maker_FeatherWing/Prop_Maker_LED_Simpletest/Prop_Maker_LED_Simpletest.ino

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@
6262

6363
uint8_t i=0;
6464

65+
uint8_t red_out = RED_LED;
66+
uint8_t green_out = GREEN_LED;
67+
uint8_t blue_out = BLUE_LED;
68+
6569
void setup() {
6670
Serial.begin(115200);
6771
Serial.println("\nProp-Maker Wing: LED Example");
@@ -73,21 +77,32 @@ void setup() {
7377

7478
// Set up the LED Pins
7579
#if defined(ESP32) // and ESP32-S2!
76-
ledcSetup(RED_LED, 5000, 8);
77-
ledcAttachPin(RED_PIN, RED_LED);
78-
ledcSetup(GREEN_LED, 5000, 8);
79-
ledcAttachPin(GREEN_PIN, GREEN_LED);
80-
ledcSetup(BLUE_LED, 5000, 8);
81-
ledcAttachPin(BLUE_PIN, BLUE_LED);
80+
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 1)
81+
// newer LEDC API, use pins instead of channel
82+
red_out = RED_PIN;
83+
green_out = GREEN_PIN;
84+
blue_out = BLUE_PIN;
85+
ledcAttach(RED_PIN, 5000, 8);
86+
ledcAttach(GREEN_PIN, 5000, 8);
87+
ledcAttach(BLUE_PIN, 5000, 8);
88+
#else
89+
// older LEDC API, use channel, attach pin to channel
90+
ledcSetup(RED_LED, 5000, 8);
91+
ledcAttachPin(RED_PIN, RED_LED);
92+
ledcSetup(GREEN_LED, 5000, 8);
93+
ledcAttachPin(GREEN_PIN, GREEN_LED);
94+
ledcSetup(BLUE_LED, 5000, 8);
95+
ledcAttachPin(BLUE_PIN, BLUE_LED);
96+
#endif
8297
#else
83-
pinMode(RED_LED, OUTPUT);
84-
pinMode(GREEN_LED, OUTPUT);
85-
pinMode(BLUE_LED, OUTPUT);
98+
pinMode(red_out, OUTPUT);
99+
pinMode(green_out, OUTPUT);
100+
pinMode(blue_out, OUTPUT);
86101
#endif
87102

88-
analogWrite(RED_LED, 0);
89-
analogWrite(GREEN_LED, 0);
90-
analogWrite(BLUE_LED, 0);
103+
analogWrite(red_out, 0);
104+
analogWrite(green_out, 0);
105+
analogWrite(blue_out, 0);
91106
}
92107

93108
uint32_t Color(uint8_t r, uint8_t g, uint8_t b) {
@@ -119,8 +134,8 @@ void loop()
119134
digitalWrite(POWER_PIN, HIGH);
120135

121136
// write colors to the 3W LED
122-
analogWrite(RED_LED, red);
123-
analogWrite(GREEN_LED, green);
124-
analogWrite(BLUE_LED, blue);
137+
analogWrite(red_out, red);
138+
analogWrite(green_out, green);
139+
analogWrite(blue_out, blue);
125140
delay(2);
126-
}
141+
}

FunHouse_Arduino_Demos/rainbow/rainbow.ino

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,31 @@ uint8_t LED_dutycycle = 0;
1414

1515
void setup() {
1616
Serial.begin(115200);
17-
17+
1818
pinMode(LED_BUILTIN, OUTPUT);
19-
19+
20+
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 1)
21+
ledcAttach(LED_BUILTIN, 5000, 8);
22+
#else
2023
ledcSetup(0, 5000, 8);
2124
ledcAttachPin(LED_BUILTIN, 0);
22-
25+
#endif
26+
2327
pixels.begin(); // Initialize pins for output
2428
pixels.show(); // Turn all LEDs off ASAP
2529
pixels.setBrightness(20);
2630
}
2731

2832

29-
3033
void loop() {
3134
Serial.println("Hello!");
3235

3336
// pulse red LED
37+
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 1)
38+
ledcWrite(LED_BUILTIN, LED_dutycycle++);
39+
#else
3440
ledcWrite(0, LED_dutycycle++);
41+
#endif
3542

3643
// rainbow dotstars
3744
for (int i=0; i<pixels.numPixels(); i++) { // For each pixel in strip...
@@ -54,4 +61,4 @@ void rainbow(int wait) {
5461
pixels.show(); // Update strip with new contents
5562
delay(wait); // Pause for a moment
5663
}
57-
}
64+
}

FunHouse_Arduino_Demos/selftest/selftest.ino

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ void setup() {
2727
//while (!Serial);
2828
Serial.begin(115200);
2929
delay(100);
30-
30+
3131
pixels.begin(); // Initialize pins for output
3232
pixels.show(); // Turn all LEDs off ASAP
3333
pixels.setBrightness(20);
@@ -37,7 +37,7 @@ void setup() {
3737
pinMode(BUTTON_UP, INPUT_PULLDOWN);
3838

3939
//analogReadResolution(13);
40-
40+
4141
tft.init(240, 240); // Initialize ST7789 screen
4242
pinMode(TFT_BACKLIGHT, OUTPUT);
4343
digitalWrite(TFT_BACKLIGHT, HIGH); // Backlight on
@@ -52,8 +52,8 @@ void setup() {
5252
tft.setTextColor(ST77XX_YELLOW);
5353
tft.print("DP310? ");
5454

55-
56-
if (! dps.begin_I2C()) {
55+
56+
if (! dps.begin_I2C()) {
5757
tft.setTextColor(ST77XX_RED);
5858
tft.println("FAIL!");
5959
while (1) delay(100);
@@ -67,8 +67,8 @@ void setup() {
6767
tft.setCursor(0, 20);
6868
tft.setTextColor(ST77XX_YELLOW);
6969
tft.print("AHT20? ");
70-
71-
if (! aht.begin()) {
70+
71+
if (! aht.begin()) {
7272
tft.setTextColor(ST77XX_RED);
7373
tft.println("FAIL!");
7474
while (1) delay(100);
@@ -79,12 +79,18 @@ void setup() {
7979
pinMode(LED_BUILTIN, OUTPUT);
8080
pinMode(SPEAKER, OUTPUT);
8181

82+
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 1)
83+
ledcAttach(LED_BUILTIN, 2000, 8);
84+
ledcAttach(SPEAKER, 2000, 8);
85+
ledcWrite(SPEAKER, 0);
86+
#else
8287
ledcSetup(0, 2000, 8);
8388
ledcAttachPin(LED_BUILTIN, 0);
8489

8590
ledcSetup(1, 2000, 8);
8691
ledcAttachPin(SPEAKER, 1);
8792
ledcWrite(1, 0);
93+
#endif
8894
}
8995

9096

@@ -94,11 +100,11 @@ void loop() {
94100

95101
/********************* sensors */
96102
sensors_event_t humidity, temp, pressure;
97-
103+
98104
tft.setCursor(0, 0);
99105
tft.setTextColor(ST77XX_YELLOW, BG_COLOR);
100106
dps.getEvents(&temp, &pressure);
101-
107+
102108
tft.print("DP310: ");
103109
tft.print(temp.temperature, 0);
104110
tft.print(" C ");
@@ -125,23 +131,23 @@ void loop() {
125131
tft.setCursor(0, 40);
126132
tft.setTextColor(ST77XX_YELLOW);
127133
tft.print("Buttons: ");
128-
if (! digitalRead(BUTTON_DOWN)) {
134+
if (! digitalRead(BUTTON_DOWN)) {
129135
tft.setTextColor(ST77XX_GREY);
130136
} else {
131137
Serial.println("DOWN pressed");
132138
tft.setTextColor(ST77XX_WHITE);
133139
}
134140
tft.print("DOWN ");
135141

136-
if (! digitalRead(BUTTON_SELECT)) {
142+
if (! digitalRead(BUTTON_SELECT)) {
137143
tft.setTextColor(ST77XX_GREY);
138144
} else {
139145
Serial.println("SELECT pressed");
140146
tft.setTextColor(ST77XX_WHITE);
141147
}
142148
tft.print("SEL ");
143-
144-
if (! digitalRead(BUTTON_UP)) {
149+
150+
if (! digitalRead(BUTTON_UP)) {
145151
tft.setTextColor(ST77XX_GREY);
146152
} else {
147153
Serial.println("UP pressed");
@@ -151,25 +157,25 @@ void loop() {
151157

152158
/************************** CAPACITIVE */
153159
uint16_t touchread;
154-
160+
155161
tft.setCursor(0, 60);
156162
tft.setTextColor(ST77XX_YELLOW, BG_COLOR);
157163
tft.print("Captouch 6: ");
158164
touchread = touchRead(6);
159-
if (touchread < 10000 ) {
165+
if (touchread < 10000 ) {
160166
tft.setTextColor(ST77XX_GREY, BG_COLOR);
161167
} else {
162168
tft.setTextColor(ST77XX_WHITE, BG_COLOR);
163169
}
164170
tft.print(touchread);
165171
tft.println(" ");
166172
Serial.printf("Captouch #6 reading: %d\n", touchread);
167-
173+
168174
tft.setCursor(0, 80);
169175
tft.setTextColor(ST77XX_YELLOW, BG_COLOR);
170176
tft.print("Captouch 7: ");
171177
touchread = touchRead(7);
172-
if (touchread < 20000 ) {
178+
if (touchread < 20000 ) {
173179
tft.setTextColor(ST77XX_GREY, BG_COLOR);
174180
} else {
175181
tft.setTextColor(ST77XX_WHITE, BG_COLOR);
@@ -183,7 +189,7 @@ void loop() {
183189
tft.setTextColor(ST77XX_YELLOW, BG_COLOR);
184190
tft.print("Captouch 8: ");
185191
touchread = touchRead(8);
186-
if (touchread < 20000 ) {
192+
if (touchread < 20000 ) {
187193
tft.setTextColor(ST77XX_GREY, BG_COLOR);
188194
} else {
189195
tft.setTextColor(ST77XX_WHITE, BG_COLOR);
@@ -200,7 +206,7 @@ void loop() {
200206
tft.setTextColor(ST77XX_YELLOW, BG_COLOR);
201207
tft.print("Analog 0: ");
202208
analogread = analogRead(A0);
203-
if (analogread < 8000 ) {
209+
if (analogread < 8000 ) {
204210
tft.setTextColor(ST77XX_WHITE, BG_COLOR);
205211
} else {
206212
tft.setTextColor(ST77XX_RED, BG_COLOR);
@@ -214,7 +220,7 @@ void loop() {
214220
tft.setTextColor(ST77XX_YELLOW, BG_COLOR);
215221
tft.print("Analog 1: ");
216222
analogread = analogRead(A1);
217-
if (analogread < 8000 ) {
223+
if (analogread < 8000 ) {
218224
tft.setTextColor(ST77XX_WHITE, BG_COLOR);
219225
} else {
220226
tft.setTextColor(ST77XX_RED, BG_COLOR);
@@ -223,12 +229,12 @@ void loop() {
223229
tft.println(" ");
224230
Serial.printf("Analog A1 reading: %d\n", analogread);
225231

226-
232+
227233
tft.setCursor(0, 160);
228234
tft.setTextColor(ST77XX_YELLOW, BG_COLOR);
229235
tft.print("Analog 2: ");
230236
analogread = analogRead(A2);
231-
if (analogread < 8000 ) {
237+
if (analogread < 8000 ) {
232238
tft.setTextColor(ST77XX_WHITE, BG_COLOR);
233239
} else {
234240
tft.setTextColor(ST77XX_RED, BG_COLOR);
@@ -245,21 +251,25 @@ void loop() {
245251
tft.print(analogread);
246252
tft.println(" ");
247253
Serial.printf("Light sensor reading: %d\n", analogread);
248-
254+
249255
/************************** Beep! */
250-
if (digitalRead(BUTTON_SELECT)) {
256+
if (digitalRead(BUTTON_SELECT)) {
251257
Serial.println("** Beep! ***");
252258
fhtone(SPEAKER, 988.0, 100.0); // tone1 - B5
253259
fhtone(SPEAKER, 1319.0, 200.0); // tone2 - E6
254260
delay(100);
255261
//fhtone(SPEAKER, 2000.0, 100.0);
256262
}
257-
263+
258264
/************************** LEDs */
259265
// pulse red LED
266+
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 1)
267+
ledcWrite(LED_BUILTIN, LED_dutycycle);
268+
#else
260269
ledcWrite(0, LED_dutycycle);
270+
#endif
261271
LED_dutycycle += 32;
262-
272+
263273
// rainbow dotstars
264274
for (int i=0; i<pixels.numPixels(); i++) { // For each pixel in strip...
265275
int pixelHue = firstPixelHue + (i * 65536L / pixels.numPixels());
@@ -271,9 +281,16 @@ void loop() {
271281

272282

273283
void fhtone(uint8_t pin, float frequency, float duration) {
284+
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 1)
285+
ledcAttach(SPEAKER, frequency, 8);
286+
ledcWrite(SPEAKER, 128);
287+
delay(duration);
288+
ledcWrite(SPEAKER, 0);
289+
#else
274290
ledcSetup(1, frequency, 8);
275291
ledcAttachPin(pin, 1);
276292
ledcWrite(1, 128);
277293
delay(duration);
278294
ledcWrite(1, 0);
295+
#endif
279296
}

0 commit comments

Comments
 (0)