go-rpio/examples/pwm/pwm.go

43 lines
1.1 KiB
Go
Raw Normal View History

2018-01-12 01:16:40 +01:00
/*
2018-01-12 15:35:22 +01:00
A PWM example by @Drahoslav7, using the go-rpio library
2018-01-12 01:16:40 +01:00
Toggles a LED on physical pin 19 (mcu pin 10)
Connect a LED with resistor from pin 19 to ground.
*/
package main
import (
"os"
"time"
"github.com/stianeikeland/go-rpio"
)
func main() {
err := rpio.Open()
if err != nil {
os.Exit(1)
}
defer rpio.Close()
pin := rpio.Pin(19)
pin.Mode(rpio.Pwm)
2018-01-12 15:35:22 +01:00
pin.Freq(64000)
2018-01-12 01:16:40 +01:00
pin.DutyCycle(0, 32)
2018-01-12 15:35:22 +01:00
// the LED will be blinking at 2000Hz
// (source frequency divided by cycle length => 64000/32 = 2000)
2018-01-12 01:16:40 +01:00
2018-01-12 15:35:22 +01:00
// five times smoothly fade in and out
2018-01-12 01:16:40 +01:00
for i := 0; i < 5; i++ {
for i := uint32(0); i < 32; i++ { // increasing brightness
pin.DutyCycle(i, 32)
time.Sleep(time.Second/32)
}
2018-01-12 15:35:22 +01:00
for i := uint32(32); i > 0; i-- { // decreasing brightness
2018-01-12 01:16:40 +01:00
pin.DutyCycle(i, 32)
time.Sleep(time.Second/32)
}
}
}