You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
+
42
41
ch, err:= pwm.Channel(pin)
43
42
if err != nil {
44
43
println(err.Error())
44
+
return
45
45
}
46
+
46
47
for {
47
48
fori:=1; i < 255; i++ {
48
49
// This performs a stylish fade-out blink
@@ -75,39 +76,23 @@ led := machine.LED
75
76
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.
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
87
80
```
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.
89
82
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).
94
84
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.
101
86
102
87
```go
103
88
varperioduint64 = 1e9 / 500
104
89
```
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.
106
91
107
92
The period of a PWM signal is the time between rising edges.
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.
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!
157
142
158
143
## 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.
160
146
161
147
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