Skip to content

Commit 9ddd47d

Browse files
committed
docs/tutorials: update PWM tutorial to match current API
Signed-off-by: deadprogram <ron@hybridgroup.com>
1 parent 9970c04 commit 9ddd47d

File tree

1 file changed

+20
-34
lines changed

1 file changed

+20
-34
lines changed

content/docs/tutorials/pwm.md

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,24 @@ import (
2626
"time"
2727
)
2828

29+
var period uint64 = 1e9 / 500
30+
2931
func main() {
30-
pin := machine.LED
3132
// This program is specific to the Raspberry Pi Pico.
32-
pin.Configure(machine.PinConfig{Mode: machine.PinPWM})
33-
pwm, err := machine.NewPWM(pin)
34-
if err != nil {
35-
println(err.Error())
36-
}
37-
var period uint64 = 1e9 / 500
38-
err = pwm.Configure(machine.PWMConfig{Period: period})
39-
if err != nil {
40-
println(err.Error())
41-
}
33+
pin := machine.LED
34+
pwm := machine.PWM4 // Pin 25 (LED on pico) corresponds to PWM4.
35+
36+
// Configure the PWM with the given period.
37+
pwm.Configure(machine.PWMConfig{
38+
Period: period,
39+
})
40+
4241
ch, err := pwm.Channel(pin)
4342
if err != nil {
4443
println(err.Error())
44+
return
4545
}
46+
4647
for {
4748
for i := 1; i < 255; i++ {
4849
// This performs a stylish fade-out blink
@@ -75,39 +76,23 @@ led := machine.LED
7576
This declares a new variable named `led`. If you are not familiar with Go, the `:=` operator is somewhat like `auto` in C++: it declares a new variable while inferencing the type from the right hand size. In this case, this is the `machine.LED` constant which has type `machine.Pin`. Boards which have an on-board LED will have this constant available for use.
7677

7778
```go
78-
pin.Configure(machine.PinConfig{Mode: machine.PinPWM})
79-
```
80-
This code configures the GPIO pin. Like most peripherals in the machine package, GPIO pins need initialization before they can be used. Maybe the following is clearer if you’re not used to this syntax:
81-
82-
```go
83-
config := machine.PinConfig{
84-
Mode: machine.PinPWM
85-
}
86-
pin.Configure(config)
79+
pwm := machine.PWM4
8780
```
88-
The first three lines define a new `machine.PinConfig` object and sets the `Mode` field to `machine.PinPWM`. This means that `config.Mode` will now equal `machine.PinPWM`.
81+
Now things start to look quite different to the blinky example. This code creates a variable which references a PWM peripheral.
8982

90-
```go
91-
pwm, err := machine.NewPWM(pin)
92-
```
93-
Now things start to look quite different to the blinky example. This code creates a variable which references the PWM peripheral. A _peripheral_ refers to any on-board integrated circuit which interfaces directly with the microprocessor and has I/O capabilities. Some other tasks which different peripherals may manage are communications ([I2C](https://en.wikipedia.org/wiki/I%C2%B2C), [SPI](https://en.wikipedia.org/wiki/Serial_Peripheral_Interface), UART, etc.) and [ADC](https://en.wikipedia.org/wiki/Analog-to-digital_converter)/[DAC](https://en.wikipedia.org/wiki/Digital-to-analog_converter).
83+
A _peripheral_ refers to any on-board integrated circuit which interfaces directly with the microprocessor and has I/O capabilities. Some other tasks which different peripherals may manage are communications ([I2C](https://en.wikipedia.org/wiki/I%C2%B2C), [SPI](https://en.wikipedia.org/wiki/Serial_Peripheral_Interface), UART, etc.) and [ADC](https://en.wikipedia.org/wiki/Analog-to-digital_converter)/[DAC](https://en.wikipedia.org/wiki/Digital-to-analog_converter).
9484

95-
```go
96-
if err != nil {
97-
println(err.Error())
98-
}
99-
```
100-
This code checks if there was an error and prints it. It is quite common in Go to come across this pattern though usually the error is handled and not simply printed.
85+
In the case of a PWM peripheral, there is the additional idea of a channel which can be used to control more than one pin at the same time with a single PWM peripheral.
10186

10287
```go
10388
var period uint64 = 1e9 / 500
10489
```
105-
We declare a new variable to calculate and store the PWM period of 2 milliseconds. By convention periods are unsigned integers which represent a duration in _nanoseconds_, also written as _ns_. The formula used above is `period = 1 / frequency`, where `500` is the desired frequency in hertz (periods per second) and `1e9` is a conversion constant to convert a period in seconds to nanoseconds.
90+
We previously declared a new variable to calculate and store the PWM period of 2 milliseconds. By convention periods are unsigned integers which represent a duration in _nanoseconds_, also written as _ns_. The formula used above is `period = 1 / frequency`, where `500` is the desired frequency in hertz (periods per second) and `1e9` is a conversion constant to convert a period in seconds to nanoseconds.
10691

10792
The period of a PWM signal is the time between rising edges.
10893

10994
```go
110-
err = pwm.Configure(machine.PWMConfig{Period: period})
95+
pwm.Configure(machine.PWMConfig{Period: period})
11196
```
11297
This code configures the PWM peripheral with the desired period. Keep in mind the output period of the PWM is usually not exact and may differ between microcontrollers.
11398

@@ -156,6 +141,7 @@ time.Sleep(time.Millisecond * 5)
156141
This code stops the program for 5 milliseconds and does nothing for that duration. It is used to slow down the rate at which the LED blinks. If it were not present then we would not notice the fade out effect because it would happen too fast to be perceived!
157142

158143
## Final thoughts
159-
Although PWM may have it's ins and outs which make it a complex topic to explain thoroughly, the program shown has everything one needs to begin harnessing the power of PWM control.
144+
145+
Although PWM may have its ups and downs which make it a complex topic to explain thoroughly, the program shown has everything one needs to begin harnessing the power of PWM control.
160146

161147
I hope this tutorial was helpful. If you have any questions, feel free to join the #tinygo channel on the [Gophers Slack](https://invite.slack.golangbridge.org/).

0 commit comments

Comments
 (0)