Comments (8)
this is my golang codes using pwm to control motor, FYI.
https://github.com/shanghuiyang/rpi-devices/blob/master/app/car/car.go
from go-rpio.
Hi,
in PWM mode the freq
method does not set the output frequency, but the base clock frequency of the channel. You have to multiply it by the cycle length (because the output frequency is given by base clock frequency divided by cycle length as it is written in the doc).
This should work:
pin.Mode(rpio.Pwm)
pin.Freq(50 * 50)
pin.DutyCycle(0, 50)
...
pin.DutyCycle(7, 50)
or this:
pin.Mode(rpio.Pwm)
pin.Freq(50 * 100)
pin.DutyCycle(0, 100)
...
pin.DutyCycle(15, 100) // this will give you the 7.5/50 ratio you have in your python code
from go-rpio.
I have similar issue. I try to control electric motor speed with hardware PWM.
Just to test that go-rpio is working I created a simple example which suppose to make motor spinning at ~75% of max RPM:
if err := gpio.Open(); err != nil {
log.Printf("Error opening GPIO: %s", err.Error())
return err
}
defer func() {
gpio.StopPwm()
gpio.Close()
}()
pin := gpio.Pin(18)
pin.Pwm()
pin.DutyCycle(3, 4)
pin.Freq(5000 * 4) // 5000Hz * 4 cycle length
gpio.StartPwm()
time.Sleep(10 * time.Second)
Running this code has zero effect.
Could you pls help me to troubleshoot it?
from go-rpio.
- What Raspberry pi generation you have? There might be some incompatibility witch the newest version.
- What number is your
pin
? Only some pins support the PWM mode. - Are you sure that 5000Hz s what your motor needs to operate? (edited)
- Are you running the code as root? PWM modes require that.
from go-rpio.
@drahoslove thank you for quick response.
- I use Raspberry Pi 4 Model B 2 GB
- As you can see form my example - I use pin 18 which suppose to support hardware PWM. I checked the wiring and it is correct. I see voltage change if I set this pin to
output
andhigh
- As for freq - it would be easier for me to run pwm signal at lower frequencies but I found a comment in go-rpio code saying
Param freq should be in range 4688Hz - 19.2MHz to prevent unexpected behavior
so I set frequency to 5kHz to stay within recommended range. My motor should work fine on those frequencies. - I tried both root and non-root. I found that if I just set pin to
high
withpin.High()
function call - it doesn't work when I run my app withsudo
but works fine without it.
For PWM - I don't see any signal coming out either way: with or withoutsudo
from go-rpio.
@AndrewKovalenko
The RPi 4 model is not fully supported, but I merged the pull-request #50 which might solve your issue. Please try it.
from go-rpio.
@drahoslove thank you. I just tried to pull the latest - it seems like I still have an issue.
To exclude the problem with the motor I connected an LED instead of motor and control it from pin 18 via 2n222 transistor.
Here is a listing of my "testing app":
package main
import (
"log"
"time"
gpio "github.com/stianeikeland/go-rpio/v4"
)
const cycleLength = 100
const pmwClockFrequency = 50 * cycleLength // 50kHz
func main() {
if err := gpio.Open(); err != nil {
log.Fatalf("Error opening GPIO: %s", err.Error())
}
defer func() {
log.Println("Stop PWM")
gpio.StopPwm()
gpio.Close()
}()
pin := gpio.Pin(18)
pin.Output()
for i := 0; i < 10; i++ {
log.Printf("Toggle pin. i=%d \n", i)
pin.Toggle()
time.Sleep(time.Second)
}
pin.Pwm()
pin.Freq(pmwClockFrequency)
log.Println("Start PWM")
gpio.StartPwm()
log.Println("10%")
pin.DutyCycle(10, cycleLength)
time.Sleep(3 * time.Second)
log.Println("30%")
pin.DutyCycle(30, cycleLength)
time.Sleep(3 * time.Second)
log.Println("50%")
pin.DutyCycle(50, cycleLength)
time.Sleep(3 * time.Second)
log.Println("70%")
pin.DutyCycle(70, cycleLength)
time.Sleep(3 * time.Second)
log.Println("100%")
pin.DutyCycle(100, cycleLength)
time.Sleep(10 * time.Second)
}
The interesting issue I observe is:
when I run this app for the first time - LED is blinking while the pin is in output
mode and then nothing happens in PWM mode. However if I run the same app second time - LED doesn't blink any more. It just turns on for 10 sec and goes off once pin is set to PWM. This issue persists until I reboot RasPi.
from go-rpio.
related #68
from go-rpio.
Related Issues (20)
- Unable To Find Compatible WiringPi Pinouts With This Library HOT 4
- raspbery pi 3+ b HOT 1
- strangely, does *not* work with sudo HOT 8
- SPI Locking? HOT 2
- Nice library but seems PWM is behaving a bit strangely HOT 3
- Dockerize? HOT 3
- [BUG] Issues using SPI and GPIO with HOT 1
- RPI 4 B PWM not working HOT 8
- cannot find package "github.com/stianeikeland/go-rpio/v4" HOT 1
- pullup does not work on RPI 4 B HOT 4
- Seems not work on Raspberry Board 4 and ubuntu server 20.04 HOT 1
- New release HOT 1
- Pico HOT 1
- Pi4 PWM frequency wrong HOT 2
- Add support for PWM "Balanced" algorithm (default BCM mode) HOT 8
- Hardlock when EdgeDetect is used HOT 5
- "Undefined: syscall"
- How to call async
- Interrupt/event based edge detection HOT 1
- seg fault on Read() HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go-rpio.