Page 1 of 2

### Generate 2 pwm signals

Posted: Tue Mar 12, 2013 3:22 pm
Hi,

I'm actually working on a project to build a small automated robot based on 2WD Arduino platform and I'm working right now on the motor shield schematics. I would like to control two different speed for the motors but the fact is, the Raspberry can't generate two PWM signals. So do you know a way to create your own PWM signal ? By that I mean, really create the signals, not software PWM or buying some boards on internet.

If I could have some informations about how to programmatically control two different PWM signals it would be great.

Thank you,

AwaX

### Re: Generate 2 pwm signals

Posted: Tue Mar 12, 2013 3:49 pm
I don't understand the distinction you are making between programmatically and software PWM.

There are quite a few solutions which use DMA to produce the timed PWM pulses.

E.g. for C, http://www.raspberrypi.org/phpBB3/viewt ... p?p=265624

### Re: Generate 2 pwm signals

Posted: Tue Mar 12, 2013 3:58 pm
There are (as always) several options, I will describe one analogue and one digital method.
For the analogue circuit you need a triangle wave generator (or saw tooth,it does not matter), a D->A converter and an analogue comparator. You set the waveform generator to run at the recommended rate for the motor, route its output to one input of the comparator. Take a parallel digital output form the controller (several RPi outputs or a single out clocked into a shift register) via the A->D then to the other input of the comparator. The output of the comparator is the required PWM signal.
The all digital system uses a fast clock (for 8 bit resolution it would be base freq*256) use this as the input to an 8 bit counter then use a digital comparator with the the 8 bit output from the controller (RPi as above). The output of the comparator is the PWM signal.

The beauty of the digital system is the if you have a much faster clock and a wider counter you can deumx the comparator output to several PWM signals, thereby using only 1 clock, counter and comparator. This was the solution I used to build a 1,000 channel lighting system way back in the 80's when digits and lights werent good friends)

### Re: Generate 2 pwm signals

Posted: Tue Mar 12, 2013 4:00 pm
The PI actually has three PWM hardware engines routed out, but two of them are used for stereo sound, and are driven by the GPU, not CPU. I don't think you could free them for other uses.
So no, the PI only has one useable PWM engine.
You have two choices if you need more than one PWM signal:
or
• use software PWM's

### Re: Generate 2 pwm signals

Posted: Tue Mar 12, 2013 4:53 pm
mahjongg wrote:The PI actually has three PWM hardware engines routed out, but two of them are used for stereo sound, and are driven by the GPU, not CPU. I don't think you could free them for other uses.
There are only two. PWM0, which can be mapped on GPIO18, is also used for analogue audio (right channel, according to the schematics, but actually it seems to be the left). You could certainly use PWM1 as well, except that it cannot be mapped to any of the pins of P1 or P5. You would need to mod the board to remove the audio filtering, and take the signal from there.

### Re: Generate 2 pwm signals

Posted: Tue Mar 12, 2013 9:40 pm
Okay, I did not realize that the same PWM engine that is used for the right channel (connected to the ring of the tip-ring-sleeve (TRS) 3.5mm jack plug) is the same PWM engine (PWM0) that is also used for the single "official PWM output", but routed to GPIO18.
This means that if you use GPIO18 for motor control you will possibly get strange noises from your right audio channel! Unless the same software that switches on PWM0 to GPIO18 also switches the PWM engine to GPIO40 off.

PWM1 is, as you said, only routed to the stereo audio output.
You could possibly remove R27 and pick up the PWM1 signal from there, but is a very fiddly solution indeed, and you probably need to write your own driver, plus somehow stop the GPU from driving PWM1.

### Re: Generate 2 pwm signals

Posted: Tue Mar 12, 2013 10:52 pm
If I was going to attempt the mod, I think I would remove each of R20,26 and C20,26, and remove and short out each of R21,27 and C48,34. Then the TRS socket has GPIO40,45 and GND.

Even if you are using only one PWM, and routeing it to GPIO18, it is important not to let the GPU play any analogue sound at the same time. With HDMI connected, this is easy. But with HDMI not connected, I cannot find a reliable method to disable all analogue audio.

### Re: Generate 2 pwm signals

Posted: Wed Mar 13, 2013 8:25 am
@joan :
It's true that my question might not be clear. By software PWM, I mean that you have to write a program (maybe C program) to generate a PWM signal on one GPIO output but it's not what I want to do. What I want to do is to programmatically control the board that generate the PWM signals, and by that I mean that you have to give a speed value (a program running on the Raspberry) that control a motor shield to effectively give that speed on the motors. So I would have to create a shield for my motors and be able to give a speed as input.

@mahjongg & jojopi :
I heard about your solution to use the PWM signal from the audio channel, it seems to effectively generates some strange noise on the audio channels. But this solution seems to be a little bit tricky for me, I think that it would be better in my case to create my own reusable shield.

@aTao :
Your analogue solution seems to be the best for me, I just need to completely understand what you're saying haha.
1 - Triangle wave generator :
Is a NE555 will do the job ? But how could I digitally control the rate ? Because in that case you have to control the input voltage.
2 - Analogue comparator :
Ok, it seems that I only need an ANx IC component ? You give two input signals to compare and you have '1' or '0' on your output (aka your PWM signal).
3 - Input of the analogue comparator :
So you have on one hand the output of the wave generator which give the pulse command for the PWM signal and on the other hand a signal generated by the DAC. But what is the role of the DAC because you just need the generator to give a controlled pulse ? When you wrote D->A, it means Digital to Analogue Converter, right ?

By the way, thanks a lot for your responses ! It's really great to have some people to help

AwaX

### Re: Generate 2 pwm signals

Posted: Wed Mar 13, 2013 5:12 pm
Philips (NXP) makes a whole range of PWM LED controllers that interface with I2C, these are also useable for all kinds of other PWM applications.

### Re: Generate 2 pwm signals

Posted: Wed Mar 13, 2013 6:00 pm
We have been using the Adafruit PWM 16 board that gives you 16 PWM outputs with the 2 wire I2C connection.

That gives us plenty of pwms to control two motors and numerous servos to control a robot arm.

Here a tutorial on using it with the Pi: http://learn.adafruit.com/adafruit-16-c ... pberry-pi/

They use Python to program it, but we use Java and found a driver for that as well.

Rick

### Re: Generate 2 pwm signals

Posted: Wed Mar 13, 2013 6:21 pm
Thanks a lot but I would like to make it myself because if I buy it I would't learn anything. So that's why I'm looking for some help to make my own PWM signal

The aTao solution seems to be the perfect way to do it but I need more details to make it.

### Re: Generate 2 pwm signals

Posted: Wed Mar 13, 2013 6:41 pm
AwaX wrote:Thanks a lot but I would like to make it myself because if I buy it I would't learn anything. So that's why I'm looking for some help to make my own PWM signal

The aTao solution seems to be the perfect way to do it but I need more details to make it.
OK, which way do you want to go, analogue or digital?

### Re: Generate 2 pwm signals

Posted: Wed Mar 13, 2013 6:58 pm
Analogue (if it's possible to interface the Raspberry with the board and give a speed command or something like that).

But I replied to your proposition just above:)

### Re: Generate 2 pwm signals

Posted: Wed Mar 13, 2013 7:33 pm
to control it analog the PI would need to have to output an analog system, but the PI doesn't have them!
To get them you again need to control an DAC chip through one of the digital signals, (I2S or SPI) which doesn't make sense!

A simple way to make analog would be through an R2R "ladder network", but it would use a GPIO pin for each bit of the D/A converter, that doesn't make much sense either.

Again your best bet would be to use a PWM driver chip, exactly like the adaboard uses!
You can get them in DIP form, so no small part soldering skills are needed.

### Re: Generate 2 pwm signals

Posted: Wed Mar 13, 2013 7:47 pm

And is it difficult to make exactly the same as the adaboard ? I mean create a SPI interface and generate the PWM signals ?

### Re: Generate 2 pwm signals

Posted: Wed Mar 13, 2013 11:26 pm
AwaX wrote:Analogue (if it's possible to interface the Raspberry with the board and give a speed command or something like that).

But I replied to your proposition just above:)
Sorry, I missed that...

AwaX wrote:I just need to completely understand what you're saying haha.
1 - Triangle wave generator :
Is a NE555 will do the job ? But how could I digitally control the rate ? Because in that case you have to control the input voltage.
2 - Analogue comparator :
Ok, it seems that I only need an ANx IC component ? You give two input signals to compare and you have '1' or '0' on your output (aka your PWM signal).
3 - Input of the analogue comparator :
So you have on one hand the output of the wave generator which give the pulse command for the PWM signal and on the other hand a signal generated by the DAC. But what is the role of the DAC because you just need the generator to give a controlled pulse ? When you wrote D->A, it means Digital to Analogue Converter, right ?

By the way, thanks a lot for your responses ! It's really great to have some people to help

AwaX
1 NE555 is ideal, use a fixed frequency triangle circuit as per http://electroschematics.com/5004/555-t ... generator/. Variable rate would be a bit trickier and require more control. Can have another idea if you really want this.

2 Correct.

3 The D->A converter is how the RPi sets the PWM ratio. For this there are several solutions ranging from R,2R ladders on many outputs of the RPi to I2C chips that just plug right in.

### Re: Generate 2 pwm signals

Posted: Thu Mar 14, 2013 10:36 am
So if I understand correctly, the NE555 give the triangle signal at a given frequency but how can the D->A converter from the RPi give the ratio ?

### Re: Generate 2 pwm signals

Posted: Thu Mar 14, 2013 10:52 am
AwaX wrote:So if I understand correctly, the NE555 give the triangle signal at a given frequency but how can the D->A converter from the RPi give the ratio ?
Ahh..
You use the comparator (eg http://www.engineersgarage.com/electron ... -datasheet) with one input from the NE555 (triangle wave) and the other from the D->A.
Lets say the RPi is trying to send a 50/50 ratio, it should output what ever signal will get the D->A converter to generate a voltage that is mid point between the high and low peaks of the triangle wave.
So... half the time the NE555 signal is higher and the other half of the time the D->A signal is higher. So, the output from the comparator will be a 50:50 PWM square wave.
Now, if the D->A output is connected to the + input of the comparator (not + supply, but the input), then as the D->A signal drops then the NE555 signal is higher for a greater percentage of the time, giving a lower mark/space ratio, similarly if the D->A gives a higher signal then you get a higher mark/space ratio.

### Re: Generate 2 pwm signals

Posted: Thu Mar 14, 2013 11:37 am
Ohh year I get it

But the problem is to control the voltage from the Raspberry, because it only give +5V. When you talk about the D->A converter it means that you have to make it ? Because the RPi doesn't have this.

### Re: Generate 2 pwm signals

Posted: Thu Mar 14, 2013 12:14 pm
AwaX wrote:Ohh year I get it

But the problem is to control the voltage from the Raspberry, because it only give +5V. When you talk about the D->A converter it means that you have to make it ? Because the RPi doesn't have this.
I use PWM to vary the voltage.

### Re: Generate 2 pwm signals

Posted: Thu Mar 14, 2013 12:16 pm
AwaX wrote:Ohh year I get it

But the problem is to control the voltage from the Raspberry, because it only give +5V. When you talk about the D->A converter it means that you have to make it ? Because the RPi doesn't have this.
Yes, you have to make it.
As always there are several options.

### Re: Generate 2 pwm signals

Posted: Thu Mar 14, 2013 12:18 pm
that's what I said, you simply move the problem from "the PI doesn't generate a second PWM signal", to "the PI doesn't generate two analog signals". Both problems need external logic, (in the latter case a lot of it), so its best to take the shortest route, and use a PWM generator chip.

### Re: Generate 2 pwm signals

Posted: Thu Mar 14, 2013 12:35 pm
Yes you're rigth, it just moves the problem... In every case I need two separated signals from the RPi but the only way to do it is to use one of the interfaces (SPI or I2C). I could use a DAC but it would use too much pins. The only way is to make a SPI or I2C interface but I think it would be difficult, isn't it ?

@joan :
I would need two differents PWM signals so we just come back to the beginning

By the way thank you very much for your involvement

### Re: Generate 2 pwm signals

Posted: Thu Mar 14, 2013 7:24 pm
from the hardware point of view its not that difficult, you just wire GND, 3V3 power and the two I2C wires to a suitable chip, which makes the PWM signals. From a software POV it means that you will need to use the I2C driver, and have to study the chip to see which registers of it to control.

A 7-PWM-channel TCA6507PW IC would be suitable, unfortunately its a SMT device!