2

I've tried to find the solution from other threads but they all seem to miss the point to a certain degree, usually going into kHz and MHz, or into the SW GPIO territory that I don't need.

I have a led controlled opto relay -setup that's not working properly and need to troubleshoot it. I'm trying to use the hardware GPIO on Raspberry Pi 1 B+. Ideally The hardware GPIO would give a PWM of about 143Hz @ 0-3.3V.

This however has proven hard, using the "gpio" utility included with WiringPi. My script:

  1. gpio pwm-ms
  2. pwmr 70
  3. pwmc 2
  4. gpio mode 1 pwm
  5. gpio pwm 1 500

What I get from that, is continuous (no oscilloscope) voltage output ~1.2V. My Fluke can measure frequencies at least up to several hundred Hz and it reports a flat 0Hz (frequency is probably in kHz, etc.) The 2 leds start functioning at around pwm 700 but there is still no frequency reported by the fluke. So I assume the frequency is way beyond my ~140Hz.

Apparently Rpi.GPIO only works in python? It seems to have a simpler way to set frequency and duty cycle.

  1. pwmNN = GPIO.PWM(ioNN, 50)
  2. pwmNN.ChangeDutyCycle(80)

I would prefer shell script and gpio utility, but why is it so hard to use?? So how would I get a frequency of 140Hz and a duty cycle of 20-80%? Reading WiringPi documentation and wiki gave me a hint that setting pwm to pwm-ms and range to 70 and divider to 2, would give me something close to 140Hz, but it doesn't seem to do so.

I haven't tried python or C, since it seems that a simple shell script (foo.sh) should do the same thing.

2

I'm sure wiringPi can do this but you can read its documentation as well as I can. I wouldn't have thought mark space versus balanced would make a difference for 143Hz.

The RPi.GPIO (Python) module is software PWM.

My pigpio library will let you generate hardware timed PWM signals.

To start a 143Hz square wave using the hardware PWM peripheral you can use the following command.

pigs hp 12 143 500000

See HP for details.

This starts a 143Hz signal on gpio 12 with dutycycle 50% (500000/1000000).

enter image description here

  • Thanks! I was looking at that pigpio library and didn't quite get it. It seems to be a more simple way to get what I want to achieve. And I didn't even know pi can also work as an oscilliscope. A lot of help. thanks again! – Kristjan Jul 29 '15 at 8:59
  • @Kristjan piscope is purely digital unlike an (anlogue) oscilloscope. I prefer to call it a digital wavefrom viewer although it could be considered a very simple logic analyser. – joan Jul 29 '15 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.