Skip to content

Commit 7ca1b6d

Browse files
committed
Add LEDC servo example and add LEDC examples to readme
1 parent 445ee30 commit 7ca1b6d

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ The valiable `YOU_WISH_TO_TRY_FILE` can be replaced with one of the following:
3939
* _wifi_connect.rb_ - Connects to WiFi. You need to replace your SSID and password in this file.
4040
* _mqtt_publish.rb_ - Publishes to MQTT broker
4141
* _filesystem.rb_ - Write/Append/Read example on the virtual filesystem
42+
* _ledc_breathe.rb_ - Gradually fades the brightness of an LED up and down
43+
* _ledc_buzzer.rb_ - Plays a melody on a pizeo-electric buzzer
44+
* _ledc_servo.rb_ - Controls position of a 180 degree hobby servo motor
4245

4346
The clean command will clean both the ESP32 build and the mruby build:
4447

main/examples/ledc_servo.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
group = ESP32::LEDC_LOW_SPEED_MODE
2+
channel = ESP32::LEDC_CHANNEL_0
3+
timer = ESP32::LEDC_TIMER_0
4+
resolution = ESP32::LEDC_TIMER_14_BIT
5+
pin = ESP32::GPIO::GPIO_NUM_4
6+
frequency = 50
7+
8+
# Configure the channel and timer.
9+
ESP32::LEDC.channel_config(pin, group, timer, channel)
10+
ESP32::LEDC.timer_config(group, timer, resolution, frequency)
11+
12+
# Using 14-bit PWM @ 50Hz, 0-16383 maps from 0 to 20 milliseconds.
13+
# Calculate how many microseconds each LSB of duty cycle represents.
14+
US_PER_BIT = (1000000.0 / frequency) / (2 ** resolution)
15+
16+
# Convert from microseconds to duty cycle.
17+
def microseconds_to_duty(us)
18+
(us / US_PER_BIT).round
19+
end
20+
21+
# This is for a 180 degree MG995 motor. Values will differ for other sweep angles,
22+
# or continuous rotation servos / ESCs. Values may vary between individual motors.
23+
#
24+
# Make sure to connect your servo motor to a separate power source!
25+
#
26+
5.times do
27+
# Send 500us pulses for 2 seconds. Should map to 0 degrees.
28+
ESP32::LEDC.set_duty(group, channel, microseconds_to_duty(500))
29+
ESP32::System.delay(2000)
30+
31+
# Send 2500us pulses for 2 seconds. Should map to 180 degrees.
32+
ESP32::LEDC.set_duty(group, channel, microseconds_to_duty(2500))
33+
ESP32::System.delay(2000)
34+
end
35+
36+
# Turn it off.
37+
ESP32::LEDC.set_duty(group, channel, 0)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy