Help with Code to Fire a magnet with PWM ( Pinball Project )

Grant666

Member
Hi All

I'm messing around with magnets for my project pinball project. I'm using Bally/Williams 20 9247 magnets off a 50VDC Supply. These measure at about 5.6ohms. Now when I fire one up I'm pulling about 8.7amps which is right according to Mr Ohms and a bit of inductive reactance of the metal core.

My problem is I could have 4 magnets active at once , which means I would need a 40amp supply, this is not good...

I was wondering if i could use pulse width modulation to run the magnets? This could reduce the power required and help cool the magnets. But I don't know if this would weaken the magnet field too much.

Like a solenoid, I would need an initial surge of full power 100%, then cut the power down to say 60%

I was going to use a 08m2 to run the transistors that will turn on the magnet.

So if anyone could lead me in the right direction with some nice code,( with some noob notes ) that would be wonderful.
Thank you
 

hippy

Technical Support
Staff member
You could probably keep track of your solenoid PWM levels, when you fire one solenoid you set its level to 100% (255) and any others which aren't off to 60% (153), update the actual PWM outputs according to their levels. Something like this for a 14M2 using four solenoids driven from hardware PWMOUT, untested -

Code:
#Picaxe 14M2

Symbol pwmLevel0 = b0
Symbol pwmLevel1 = b1
Symbol pwmLevel2 = b2
Symbol pwmLevel3 = b3

Symbol solenoid  = b4

Symbol PWM0      = B.2
Symbol PWM1      = B.4
Symbol PWM2      = C.0
Symbol PWM3      = C.2

Gosub setPwmOutputs
Do
  ; Fire the solenoids in turn
  solenoid = 0 : Gosub Fire : Pause 250
  solenoid = 1 : Gosub Fire : Pause 250
  solenoid = 2 : Gosub Fire : Pause 250
  solenoid = 3 : Gosub Fire : Pause 250
  ; Turn the solenoids off in turn
  solenoid = 0 : Gosub Done : Pause 250
  solenoid = 1 : Gosub Done : Pause 250
  solenoid = 2 : Gosub Done : Pause 250
  solenoid = 3 : Gosub Done : Pause 250
Loop

Fire:
  if pwmLevel0 > 0 Then : pwmLevel0 = 150 : End If 
  if pwmLevel1 > 0 Then : pwmLevel1 = 150 : End If 
  if pwmLevel2 > 0 Then : pwmLevel2 = 150 : End If 
  if pwmLevel3 > 0 Then : pwmLevel3 = 150 : End If 
  Select Case solenoid
    Case 0 : pwmLevel0 = 255
    Case 1 : pwmLevel1 = 255
    Case 2 : pwmLevel2 = 255
    Case 3 : pwmLevel3 = 255
  End Select
  Gosub SetPwmOutputs
  Return

Done:
  Select Case solenoid
    Case 0 : pwmLevel0 = 0
    Case 1 : pwmLevel1 = 0
    Case 2 : pwmLevel2 = 0
    Case 3 : pwmLevel3 = 0
  End Select
  Gosub SetPwmOutputs
  Return

SetPwmOutputs:
  PwmOut PWM0, 63, pwmLevel0
  PwmOut PWM0, 63, pwmLevel1
  PwmOut PWM0, 63, pwmLevel2
  PwmOut PWM0, 63, pwmLevel3
  Return
You could take the above and make a 4 channel PWM controller from a 14M2 which uses 4 digital input lines so you just turn each channel on and off in your controlling PICAXE and leave it to the 14M2 to do the actual PWM control. It would even be possible to add a timeout so channels can turn themselves off after some time if these are kickers rather than target raisers.

You could possibly bit-bang the PWM to control more solenoids. A lot would depend on how many solenoids you have and what actually works with the solenoids which will probably only be ascertained through experimenting.
 

BeanieBots

Moderator
Hope I'm not trying to teach you how to suck eggs, but PWM into large inductive will have some challenges!
For starters, the flyback diodes will need to be rated the same as the inductor forward current. ie about 10A to be safe.
What PWM frequency to use. A low frequency to keep the FETs cool will make the solenoids buzz loudly. A high frequency will require 'proper' FET drivers.

Another option might be to use two supplies diode OR'd together. One output turns on a high voltage supply, another output turns on a lower (holding) voltage supply. Both supplies are OR'd to the solenoid via diodes.
 

srnet

Senior Member
I'm using Bally/Williams 20 9247 magnets off a 50VDC Supply. These measure at about 5.6ohms. Now when I fire one up I'm pulling about 8.7amps which is right according to Mr Ohms and a bit of inductive reactance of the metal core.

My problem is I could have 4 magnets active at once , which means I would need a 40amp supply, this is not good...

I was wondering if i could use pulse width modulation to run the magnets? This could reduce the power required and help cool the magnets. But I don't know if this would weaken the magnet field too much.
Since Pin Ball Machines have been around for a very long time, I would start by assuming the solenoids and power supply are sized to do the job needed, and to cope with the heat.

PWM is not magic, you will be reducing the power that goes into the solenoids, so they are going to be less effective.

I used to work on amusement machines back in the early 80s, and the transformers to supply the many 50V solenoids were indeed quite large, they needed to be.
 

Grant666

Member
Since Pin Ball Machines have been around for a very long time, I would start by assuming the solenoids and power supply are sized to do the job needed, and to cope with the heat.

PWM is not magic, you will be reducing the power that goes into the solenoids, so they are going to be less effective.

I used to work on amusement machines back in the early 80s, and the transformers to supply the many 50V solenoids were indeed quite large, they needed to be.
Hi, I have quite a few pinballs so are familiar with their power supplies. Yes they do have magnets, but they will only run one magnet at a time, and for a short duration ( eg 4 seconds) So, yes a standard pinball supply copes with that just fine. My problem is that I want to run a few magnets at the same time, thus using lots of amps that a normal pinball supply would normally never have to do. Also you cold be right on the PWM reducing the effect of the magnet, but that's the fun of experimenting, will it work? or wont it? Just have to try and see !
 

Grant666

Member
Hope I'm not trying to teach you how to suck eggs, but PWM into large inductive will have some challenges!
For starters, the flyback diodes will need to be rated the same as the inductor forward current. ie about 10A to be safe.
What PWM frequency to use. A low frequency to keep the FETs cool will make the solenoids buzz loudly. A high frequency will require 'proper' FET drivers.

Another option might be to use two supplies diode OR'd together. One output turns on a high voltage supply, another output turns on a lower (holding) voltage supply. Both supplies are OR'd to the solenoid via diodes.
Nope, this is an egg that I have yet to suck ! Thus why I'm asking in this forum. I was using 1N4007 diodes, but might up that a bit. They work fine with just normal use, but might fail in this instant. Also I was going to use more than 1 supply, but still would like to reduce the current drain on the system when the magnets are in play. Also hadn't taken into account the "Buzz" effect.....I should have , as I'm a lift Mechanic and electrical hum in our equipment is common...and loud.. Thanks for your advice
 

Grant666

Member
You could probably keep track of your solenoid PWM levels, when you fire one solenoid you set its level to 100% (255) and any others which aren't off to 60% (153), update the actual PWM outputs according to their levels. Something like this for a 14M2 using four solenoids driven from hardware PWMOUT, untested -

Code:
#Picaxe 14M2

Symbol pwmLevel0 = b0
Symbol pwmLevel1 = b1
Symbol pwmLevel2 = b2
Symbol pwmLevel3 = b3

Symbol solenoid  = b4

Symbol PWM0      = B.2
Symbol PWM1      = B.4
Symbol PWM2      = C.0
Symbol PWM3      = C.2

Gosub setPwmOutputs
Do
  ; Fire the solenoids in turn
  solenoid = 0 : Gosub Fire : Pause 250
  solenoid = 1 : Gosub Fire : Pause 250
  solenoid = 2 : Gosub Fire : Pause 250
  solenoid = 3 : Gosub Fire : Pause 250
  ; Turn the solenoids off in turn
  solenoid = 0 : Gosub Done : Pause 250
  solenoid = 1 : Gosub Done : Pause 250
  solenoid = 2 : Gosub Done : Pause 250
  solenoid = 3 : Gosub Done : Pause 250
Loop

Fire:
  if pwmLevel0 > 0 Then : pwmLevel0 = 150 : End If 
  if pwmLevel1 > 0 Then : pwmLevel1 = 150 : End If 
  if pwmLevel2 > 0 Then : pwmLevel2 = 150 : End If 
  if pwmLevel3 > 0 Then : pwmLevel3 = 150 : End If 
  Select Case solenoid
    Case 0 : pwmLevel0 = 255
    Case 1 : pwmLevel1 = 255
    Case 2 : pwmLevel2 = 255
    Case 3 : pwmLevel3 = 255
  End Select
  Gosub SetPwmOutputs
  Return

Done:
  Select Case solenoid
    Case 0 : pwmLevel0 = 0
    Case 1 : pwmLevel1 = 0
    Case 2 : pwmLevel2 = 0
    Case 3 : pwmLevel3 = 0
  End Select
  Gosub SetPwmOutputs
  Return

SetPwmOutputs:
  PwmOut PWM0, 63, pwmLevel0
  PwmOut PWM0, 63, pwmLevel1
  PwmOut PWM0, 63, pwmLevel2
  PwmOut PWM0, 63, pwmLevel3
  Return
You could take the above and make a 4 channel PWM controller from a 14M2 which uses 4 digital input lines so you just turn each channel on and off in your controlling PICAXE and leave it to the 14M2 to do the actual PWM control. It would even be possible to add a timeout so channels can turn themselves off after some time if these are kickers rather than target raisers.

You could possibly bit-bang the PWM to control more solenoids. A lot would depend on how many solenoids you have and what actually works with the solenoids which will probably only be ascertained through experimenting.
Thanks for the Code Hippy.
I shall put together a test set up and see what happens, might release the Smoke Genie out of some of the parts. who knows ! Thanks
 

erco

Senior Member
Fun project. Dollars to doughnuts you'll have better luck sending single pulses of different durations to the coil than PWMing a solenoid. That is, switch the coil on fully for 150 ms vs 130 ms, etc. Also easier to build and program. Be sure to Google "flyback diode". :)

The forces/accelerations on a solenoid are very non-linear WRT travel, so towards the end of travel where the force is greatest, a millisecond will make more difference than at the start.
 

AllyCat

Senior Member
Hi,

The forces/accelerations on a solenoid are very non-linear WRT travel,
A simple solution might be to "dump" the energy from a large capacitor into the solenoid. Arrange the capacitor to charge via a resistor from the PSU, of such a resistance that when the solenoid is activated the resistor delivers only enough current to "hold" the solenoid on (or maintain the desired speed of travel).

Of course, using high frequency PWM is a much more "elegant" solution, but more likely to release the magic smoke, or at least Radio Interference.

Cheers, Alan.
 

rossko57

Senior Member
All sounds a bit like two well-established hardware techniques -

Model Railway (railroad?) capacitor discharge circuits, for operating solenoids that activate points (turnouts) or signals. These are usually a big current pulse and a tiny trickle when expired, on the assumption that the the point/signal has an overcentre spring to hold it in the new position. Repeat rates are limited. Possibly not what is wanted here?

Peak-and-hold solenoid driver, such as found in some automotive injector applications. A full-power pulse of short duration (milliseconds) is applied, which gets the mechanical stuff moving, followed by a more limited holding current (often around a quarter) that holds the gubbins in place when there's no mechanical inertia to overcome anymore. This has a bonus of speedier release, as well as reducing heat and power consumption. Should be effective on spring-return gubbins like you are dealing with?

That can be achieved by dedicated monostable hardware. or something like using two Picaxe outputs, one a pulsout for the highpower pulse, the other a simple on-off for a current limited drive. The current-limited drive could be via simple series resistor, or a proper electronic feedback limiter, or just driven from a lower (say 12v) supply as outlined above.
 

erco

Senior Member
A simple solution might be to "dump" the energy from a large capacitor into the solenoid.
Fabulous idea, charging a nice big cap. Solves all sort of problems. Charge the cap to a variable voltage (using ADC) then switch off charging current (or maintain voltage if for a long period) then dump. Cap is completely discharged at end, no flyback diode required, you could use even use old-school relays for charging & discharging.

Charging to different voltages gives different solenoid power levels to launch the ball.
 

Buzby

Senior Member
Those coils you have are not solenoid drivers for thumpers or flippers, they are for 'Magna Saves', which hold the ball magnetically at a fixed spot on the table.
Thumper coils and ball launchers both need a very short current pulse to kick the ball, and flipper coils have a two stage hi/lo current requirement for the kick/hold.
MagnaSaves need lots of current all the time !

Capturing a moving ball needs a huge magnetic field, but holding a static ball needs much less.
How are your coils going to be used ?. You might get away with much lower current if you are not capturing a moving ball.

Cheers,

Buzby
 

tmfkam

Senior Member
My thoughts:

Consider using opto-couplers to connect all in/outs from the PicAxe to/from the coil side of the circuit. This might help reduce problems with the coils generating large voltage/current spikes into the sensitive microcontroller (PicAxe) circuitry.
Think about using a totally separate supply, with an individual transformer purely for the PicAxe, with no common grounds of any sort. Again to reduce to a minimum, the possibility of any induced voltage/current finding it's way back to the sensitive microcontroller (PicAxe) circuitry.

Above all, have fun!

Oh, and I do like the charged capacitor suggestion. That is how most 'Electric Fence' energisers work. They have a low power step-up oscillator/transformer which charges a capacitor to a high-ish voltage (typically 200V-500V) which is then discharged into a second transformer which generates the final 5,000-10,000 volts delivered to the fence wire or tape.
 
Top