Mor Blau
Mor Blau

Reputation: 420

How do I control PWM output on Arduino using TinyGo

I'm trying to turn a motor using An Arduino ATMega2560 with code written in Go. There's an example here that uses TinyGo v0.14.1: https://create.arduino.cc/projecthub/alankrantas/tinygo-on-arduino-uno-an-introduction-6130f6

The example in essence looks like this:

func main() {
    machine.InitPWM()
    led := machine.PWM{machine.D9}
    led.Configure()
    value := 0
    led.Set(uint16(value))
}

When I try to call machine.InitPWM() I get an error InitPWM not declared by package machine

TinyGo's current version (and the one I'm running) is v0.19. It seems as though the machine package has been modified to use PWM differently, however, I cannot find anywhere how to use it correctly.

Upvotes: 2

Views: 677

Answers (2)

Jadefox10200
Jadefox10200

Reputation: 584

You must set the machine.Timer1 in order to use pin9. The below code will do what you want except that nothing will happen because 'value' is set to 0. You must use a value between 0-256 in order to do something:

pwm := machine.Timer1
pin9 := machine.D9

err := pwm.Configure(machinePWMConfig{})
if err != nil{println(err.Error())}

ch, err := pwm.Channel(pin9)
if err != nil{println(err.Error())}

//note that values are between 0-256 for pwm:
value := uint32(0)
pwm.Set(ch, uint32(value))

Upvotes: 0

astax
astax

Reputation: 1767

There is indeed no InitPWM function in machine package for ATMega2560 - https://tinygo.org/docs/reference/microcontrollers/machine/arduino-mega2560/

Upvotes: 1

Related Questions