-
Notifications
You must be signed in to change notification settings - Fork 223
/
pwm.go
43 lines (35 loc) · 1.07 KB
/
pwm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
A PWM example by @Drahoslav7, using the go-rpio library
Toggles a LED on physical pin 35 (GPIO pin 19)
Connect a LED with resistor from pin 35 to ground.
*/
package main
import (
"os"
"time"
"github.com/stianeikeland/go-rpio/v4"
)
func main() {
err := rpio.Open()
if err != nil {
os.Exit(1)
}
defer rpio.Close()
pin := rpio.Pin(19)
pin.Mode(rpio.Pwm)
pin.Freq(64000)
pin.DutyCycle(0, 32)
// the LED will be blinking at 2000Hz
// (source frequency divided by cycle length => 64000/32 = 2000)
// five times smoothly fade in and out
for i := 0; i < 5; i++ {
for i := uint32(0); i < 32; i++ { // increasing brightness
pin.DutyCycle(i, 32)
time.Sleep(time.Second/32)
}
for i := uint32(32); i > 0; i-- { // decreasing brightness
pin.DutyCycle(i, 32)
time.Sleep(time.Second/32)
}
}
}