PWM and LED Bar Graph

Hey'a guys, I'm new here and to the microcontroler world so please bare with me ;)
I'm looking to have a small circuit built that will allow me to override an external sources (an automotive ECU) control over a linear solenoid.
The ECU drives the solenoid with PWM, I used a oscilloscope to measure what the ECU sends to the solenoid, I got 1KHz, 5VDC, 999.0us, the duty cycle ranges from 0% to 50%.
When I disconnect the ECU from the solenoid, The ECU throws an error code so I've managed to fake the ECU out by placing a large 10 watt resistor in so it thinks the solenoid is still there.
It looks like I can use a pot to adjust the duty cycle out of the PICAXE, 0% to 50% to manually override and drive the solenoid.
I want to have the ability to see what the duty cycle is across a LED bar graph both when the ECU is controlling or when manual override is enabled.
I'm thinking I can use the LM3914 to drive a 10 segment LED bar graph, but from what I can tell the PICAXE can't output analog voltage, so I'm thinking I can use RC circuit with a op-amp (low-pass filter) to convert PWM to analog voltage so I can drive the LM3914. Seems like there is a more elegant way to do this all but I'm real new here so any guidance would be very much appreciated :)
 

sghioto

Senior Member
Miramax281,

Welcome to the forum. I don't see anything wrong with using a op amp in this case. Just make sure the op amp output will go to ground potential, one of those single supply rail to rail types would be my choice. The other potential problem may be the frequency of the PWM. I believe 4KHz is minimum for a Picaxe chip. Will the solenoid work on this frequency?
Some have suggested there are ways to lower this frequency through additional code.

Steve G.
 
Miramax281,

The other potential problem may be the frequency of the PWM. I believe 4KHz is minimum for a Picaxe chip. Will the solenoid work on this frequency?
Some have suggested there are ways to lower this frequency through additional code.

Steve G.
I'm pretty sure it will work with 4KHz, I'll have to do some testing to see if it effects it any. I would prefer to run at 1KHz just as the ECU delivers though...
One thing to note, this will be used in an automotive application, the circuit will be powered by the car, and from what I understand automotive power is far from clean. Can anyone recommend a good regulator that will deliver at least 1amp @ 5V and do well with the harsh automotive power?
 

westaust55

Moderator
Yes, if using the smaller PICAXE chips the slowest clock frequency is 4MHz so the lowest PWM is 3.906kHz.

PWM frequency = (PICAXE clock in Hz) / ( (1+ PWMOUT command period) * 4 )

Most likely overkill to use a 28X1, but with the 28X1/40X1 chips, then for example using the command SETFREQ m1, then a PWM frequency of ~977 Hz is achieved.
 
Yes, if using the smaller PICAXE chips the slowest clock frequency is 4MHz so the lowest PWM is 3.906kHz.

PWM frequency = (PICAXE clock in Hz) / ( (1+ PWMOUT command period) * 4 )

Most likely overkill to use a 28X1, but with the 28X1/40X1 chips, then for example using the command SETFREQ m1, then a PWM frequency of ~977 Hz is achieved.
Ah! You're right about the overkill, the 28X1 just seems a bit wasteful for this application. I'm going to have to fool around with the 4KHz mess, I don't know how it will effect the solenoid.

On the power supply note, I did run across this http://www.seetron.com/an_vpwr1.htm
Looks like the good stuff unless anyone has any better ideas ;)
 

BCJKiwi

Senior Member
For the regulator, check out the LM2940 series - specially designed for automotive use with all the relevant protection and auto supply handling issues covered. Just read the spec sheet properly and be sure to use the correct capacitors (value AND type) - not your usual electrolytics!
 

Dippy

Moderator
By poking the appropriate registers you can reduce PWM frequency to 1kHz easily.
Here is an example of the calculation...
 

Attachments

For the regulator, check out the LM2940 series - specially designed for automotive use with all the relevant protection and auto supply handling issues covered. Just read the spec sheet properly and be sure to use the correct capacitors (value AND type) - not your usual electrolytics!
This LM2940 looks pretty good, this puppy might be the route to take! :D
 

Dippy

Moderator
Firstly, just to save my typing finger, can you download the actual Microchip PIC Data Sheet.
It's a big long boring thing, but a bit handy.

I haven't looked at the 16F886 Data Sheet in detail but PICs usually use Timer2 for PWM calcs.

That image shows calcs for when doing nitty-gritty PIC programming, so it was just an example to say that 1kHz is feasible.

Anyway, to cut a long story short these 'scaler' things are didivders.

PICs are pretty consistent and the prescaler values can usually be set in register T2CON (address 12h).

Here is a little example i did with an 18X.
http://www.picaxeforum.co.uk/showthread.php?t=5872&highlight=pwm*+jeremy

(NOTE: where it says "&quot" that is where the old forum got merged into new forum. Its just a quote mark so I could output text).

Now, one thing I would generally recommend just before POKING (ooer!).
That is have a PEEK.
You never know what the firmware has set.

So, just adjust the bits you want; in this case 0 and 1.
You can do it simple arithmetic or logic.
 

hippy

Ex-Staff (retired)
@ Dippy : I changed the formatting in the link to use the new forum tags. Hope you don't mind.
 
Firstly, just to save my typing finger, can you download the actual Microchip PIC Data Sheet.
It's a big long boring thing, but a bit handy.

I haven't looked at the 16F886 Data Sheet in detail but PICs usually use Timer2 for PWM calcs.

That image shows calcs for when doing nitty-gritty PIC programming, so it was just an example to say that 1kHz is feasible.

Anyway, to cut a long story short these 'scaler' things are didivders.

PICs are pretty consistent and the prescaler values can usually be set in register T2CON (address 12h).

Here is a little example i did with an 18X.
http://www.picaxeforum.co.uk/showthread.php?t=5872&highlight=pwm*+jeremy

(NOTE: where it says "&quot" that is where the old forum got merged into new forum. Its just a quote mark so I could output text).

Now, one thing I would generally recommend just before POKING (ooer!).
That is have a PEEK.
You never know what the firmware has set.

So, just adjust the bits you want; in this case 0 and 1.
You can do it simple arithmetic or logic.
Ah! This is very good news, looks like I wont have to deviate from plan much, thanks a ton Dippy!
Now to learn more about the in's and out's of PIC programing ;)
 

boriz

Senior Member
In theory…

The PULSIN command can measure individual pulses down to 10uS resolution @ 4MHz or 5uS if the Picaxe is overclocked to 8MHz.

So, with a common ground, and the incoming PWM signal connected straight to (say) PIN1 via a protection resistor (1k should be fine), you could read the duty directly using something like this:

Code:
*pseudo code* 
Setfreq m8
Do
	Pulsin 1,1,w1
	*update display*
Loop
For a 1kHz signal @ 50% duty, w1 will contain 100 (that’s 100 * 5uS intervals). Using a 10 segment display, simply divide this by 10 to get a 1-10 range, each segment representing 5% duty. A Picaxe 14M can drive 10 LEDs using simple multiplexing.
 
In theory…

The PULSIN command can measure individual pulses down to 10uS resolution @ 4MHz or 5uS if the Picaxe is overclocked to 8MHz.

So, with a common ground, and the incoming PWM signal connected straight to (say) PIN1 via a protection resistor (1k should be fine), you could read the duty directly using something like this:

Code:
*pseudo code* 
Setfreq m8
Do
	Pulsin 1,1,w1
	*update display*
Loop
For a 1kHz signal @ 50% duty, w1 will contain 100 (that’s 100 * 5uS intervals). Using a 10 segment display, simply divide this by 10 to get a 1-10 range, each segment representing 5% duty. A Picaxe 14M can drive 10 LEDs using simple multiplexing.
I don't think I am going to use the PICAXE to measure the duty cycle after all. I'm just going to use the PICAXE to generate the 5VDC 0%-50% duty at 1KHz PWM, I will use a low-pass filter (RC circuit maybe with an op-amp) to convert the duty cycle to analog voltage and pass it along to a LM3914 (LM3915, or LM3916, not sure which one yet). Heck the more I'm thinking about it more now.. I bet I can save a few bucks if I figured out how to generate the PWM I need with a 555 based circuit...
 
A single 14m can do it all and only costs £2! You can do it cheaper another way?
I can probably get 10 555 timers for that much ;)
I would need a few diodes, caps, and resistors to go along with it, but I'm thinking its still going to be the cheaper way to go. It's just a matter of figuring out the values and such to generate a 5vdc 1KHz PWM that will range from 0% to 50%.
 

BeanieBots

Moderator
I can probably get 10 555 timers for that much ;)
I would need a few diodes, caps, and resistors to go along with it, but I'm thinking its still going to be the cheaper way to go. It's just a matter of figuring out the values and such to generate a 5vdc 1KHz PWM that will range from 0% to 50%.
And that's actually where the saving comes in.
Instead of spending the time working out values and building a much more complex circuit, spend the extra hours doing a little overtime instead. Then re-do the financial equation.

OK, as a hobby the fun is in the playing, but even then, time is still valuable.
 
And that's actually where the saving comes in.
Instead of spending the time working out values and building a much more complex circuit, spend the extra hours doing a little overtime instead. Then re-do the financial equation.

OK, as a hobby the fun is in the playing, but even then, time is still valuable.
Yeah I got ya' there. I'm going to carry forward with the PICAXE method of PWM, but will probably hire somebody to develop a cheaper PWM circuit for me if this thing works and I decide to take it to market.

One thing I'm curious of, I can't seem to find much info on picking the correct potentiometer for analog in. Looks like allot of people are using 10K pots, is this ok?
 

Dippy

Moderator
10K is fine. A lot of these values are values that have been read from PIC Data Sheets. They often whitter on about 10K source resistances and things.
Microchip are pretty conservative concerning values, so feel free to experiment.
BUT don't go too low without care, if your pot was 100 ohms (going to silly extremes) there could be a lot of supply current wanging through the pot which would be stupid.
(Yet another angle on Smoking Pot eh?)

Also, if you ever do nitty-gritty PIC programming you can adjust things sometimes to allow for component changes.

I really can't see why you are having so many problems, I don't mean to sound cheeky.
 
Well I did a bit of playing around and have had some-what luck with the code below

main:
readadc 4,w0 ' read 10k pot on pin4
w0 = w0 * 2 ' bump the duty up so we can get 50%
PWMOut 2, 249, w0 ' pulse out
Poke $12, $05 ' set t2con to :4 scale
goto main ' loop
It seems to work, but it kind of freaks my scope out, its hard to read the duty cycle and period. The pulse does not look near as clean as when its pulsing out at 4KHz. Is this type of behavior expected? Kind of looks like when the pwm out hits, its set to 4KHz, then the poke happens and it quickly changes the frequency. Kind of looks like the period is changing back and forth as the controller steps through the instructions..
 
Last edited:
Ah hah! Looks like I was right on this one. Every time pwmout is called it sets the t2con register back to default!

main:
readadc 4,w0 ' read 10k pot on pin4
w0 = w0 * 2 ' bump the duty up so we can get 50%
PWMOut 2, 249, w0 ' pulse out
Poke $12, $05 ' set t2con to :4 scale
pause 5000 ' hang on for a lil bit before loop
goto main ' loop
yay! Now, what would be the best way to set this? ;)
 
Last edited:

BCJKiwi

Senior Member
What PICAXE are you using?

28X1 and up has pwmduty which does not reset the PWM setup each time it's called.
 
What PICAXE are you using?

28X1 and up has pwmduty which does not reset the PWM setup each time it's called.
08M :)

readadc 4,w0 ' read 10k pot on pin4
w0 = w0 * 2 ' bump the duty up so we can get 50%
PWMOut 2, 1000, w0 ' pulse out
Poke $12, $05 ' set t2con to :4 scale
pause 4000 ' hang on for a lil bit before loop
goto main
 

kevrus

New Member
Not sure if this will work, hopefully the PWM will only change if there is an adjustment on the 10k pot. Works in simulator (without the POKE $12, $05)

Code:
start:
Poke $12, $05		'set t2con to :4 scale
w0=0			'ensures variable start at zero
w1=0			'ensures variable start at zero


main:
readadc 4,w0		'read 10k pot on pin4
if w1<>w0 then		'compares w1 with w0
goto solenoid_out	'goes to pulseout if PWM needs changing 
end if
goto main
 
 
solenoid_out:
w0 = w0 * 2 		'bump the duty up so we can get 50%
let w1=w0			'makes w1 = w0 to set PWM out
PWMOut 2, 249, w1 		'pulse out
pause 500			'hang on for a lil bit before loop
goto main 		'loop
 
Not sure if this will work, hopefully the PWM will only change if there is an adjustment on the 10k pot. Works in simulator (without the POKE $12, $05)

Code:
start:
Poke $12, $05		'set t2con to :4 scale
w0=0			'ensures variable start at zero
w1=0			'ensures variable start at zero


main:
readadc 4,w0		'read 10k pot on pin4
if w1<>w0 then		'compares w1 with w0
goto solenoid_out	'goes to pulseout if PWM needs changing 
end if
goto main
 
 
solenoid_out:
w0 = w0 * 2 		'bump the duty up so we can get 50%
let w1=w0			'makes w1 = w0 to set PWM out
PWMOut 2, 249, w1 		'pulse out
pause 500			'hang on for a lil bit before loop
goto main 		'loop
I see what your going for, and yes it does work. The problem is you need to poke after pwmout because pwmout sets the register back to default :eek:
 

kevrus

New Member
I see, I wasn't aware of that as i've never POKED the timer register...just noticed that you mentioned this in post #22. Perhaps I should've read the posts a bit more thoroughly...i'll blame working nightshifts!!
 

hippy

Ex-Staff (retired)
Ah hah! Looks like I was right on this one. Every time pwmout is called it sets the t2con register back to default!

yay! Now, what would be the best way to set this? ;)
Simple answer is don't use PWMOUT. Use PWMOUT once then only use PWMDUTY. If the PWMDUTY command is not available, emulate it by using POKE commands to change the PWM registers as required. You'll have to look at the PICmicro datasheet to determine what to poke and to where.
 

kevrus

New Member
Looks like the PWMDUTY is the one as BCJKiwi and Hippy said, but it would mean using a 28x1.

If you wanted to stick with the 08M then you could connect the PWM output to a 4017 decade counter, use output 1, take output 5 of the 4017 to the 4017 reset pin and you have just 'divided the PWM by 4'.

This would mean using another 'chip' though...
Unless i'm talking gibberish again as usual... :D

Mora99, that display looks cool...
 

sghioto

Senior Member
kevrus,

The 4017 will divide the base frequency by four but will not maintain the dutycycle. I tried this myself when this article was first posted.

Steve G.
 

eclectic

Moderator
@Miramax281

Wouldn't it be quicker, easier, and of a similar price,
to follow Boriz's posts #14 and #16?

Alternatively, could you live with eight LED's?
An 08M for Pwmout, also SEROUTs the Duty value to a 20M
The 20M converts the SERIN value into a light show. Job done.

Either way, you don't need the LM3914.

From your post #20
“but will probably hire somebody to develop a cheaper PWM circuit for me
if this thing works and I decide to take it to market.”

Isn't advice free on this forum?

e
 
Simple answer is don't use PWMOUT. Use PWMOUT once then only use PWMDUTY. If the PWMDUTY command is not available, emulate it by using POKE commands to change the PWM registers as required. You'll have to look at the PICmicro datasheet to determine what to poke and to where.
Check out the attached drawing for what I'm thinking.
I'm only going to use the picaxe to generate the 1KHz pulse.

Unfortunately PWMDUTY is not available with the 08M.. What registers need to be poked to emulate this? I've tried looking at the pic datasheet, but really have no idea what im looking at :eek:

 
Last edited:

hippy

Ex-Staff (retired)
Check out the attached drawing for what I'm thinking.
I'm only going to use the picaxe to generate the 1KHz pulse.
I'm a bit confused on a number of aspects of that circuit; why there needs to be 10 watt resistor, how the 7805 is used to drive the solenoid and how it interfaces to the PICAXE, and how the analogue LM3194 bar graph is to be driven by a PWM signal.

This may just be a generic block diagram and be missing a number of aspects a full circuit would have.

Unfortunately PWMDUTY is not available with the 08M.. What registers need to be poked to emulate this? I've tried looking at the pic datasheet, but really have no idea what im looking at :eek:
Off-hand I cannot answer that. Table 11-5 ("REGISTERS ASSOCIATED WITH PWM AND TIMER2") show the registers used to implement PWM, and CCPR1L and CCP1CON would seem to be the ones related to duty.

Obviously when controlling something as critical as engine management in a moving vehicle at speed it is necessary to get this absolutely correct, tested and proven before using it in an active system.

Given the nature of what you are proposing and an earlier comment about "take it to market" I am reluctant to give advice which could create a liability for myself, Rev-Ed or others in relation to the use of PICAXE's beyond what is officially supported.
 
Last edited:
I'm a bit confused on a number of aspects of that circuit; why there needs to be 10 watt resistor, how the 7805 is used to drive the solenoid and how it interfaces to the PICAXE, and how the analogue LM3194 bar graph is to be driven by a PWM signal.

This may just be a generic block diagram and be missing a number of aspects a full circuit would have.



Off-hand I cannot answer that. Table 11-5 ("REGISTERS ASSOCIATED WITH PWM AND TIMER2") show the registers used to implement PWM, and CCPR1L and CCP1CON would seem to be the ones related to duty.

Obviously when controlling something as critical as engine management in a moving vehicle at speed it is necessary to get this absolutely correct, tested and proven before using it in an active system.

Given the nature of what you are proposing and an earlier comment about "take it to market" I am reluctant to give advice which could create a liability for myself, Rev-Ed or others in relation to the use of PICAXE's beyond what is officially supported.
The 10 watt resistor is to prevent the ECU from giving an error code, its there to emulate the presence of the solenoid.

I plan to use a low-pass filter to convert the pulse out to analog voltage, that will run the LM3194, kind of making a volt meter out of the LED bar

I thought about sending the PWM out to the 7805 to drive the solenoid, but some type of mosfet transistor will probably be better.

As far as product to market, the picaxe is just a proof of concept this crazy idea. I dont see how the forum can be liable for any advice I take the wrong way :p

Anyone have any experience in changing duty cycle by poking resisters? ..maybe I should try to search before I ask :rolleyes:
 
Last edited:
Aw heck, I just realized how wacky that drawing is, I was planing on using the 5v from the LM2940 to drive the solenoid, sorry about that ;)
I need to track a good transistor or mosfet down
 
Last edited:
Can anyone recommend a good mosfet or transistor? Something the picaxe can drive, and it needs to be able to handle at least 1amp at 5vdc. The picaxe will switch it at 1KHz :D
 

boriz

Senior Member
There is a lot of crossover and most people can get away with using any one of several different power MOSFETs. I have only ever used one type and am very happy with it’s performance so far. It would work fine in your app:

STP55NF06L MOSFET LOGIC N 60V 55A
Purchased here. (Third down the list).
Datasheet here.

I’m sure different forum members have different favourites.
 
Top