Two H-Bridges for one motor ?

Following a few more back-of-the-matchbox calculations I think I will toss bit-banging in the trash.

I will have a look at chips that generate PWM. The attraction of a Mega (or a few Micros or Nanos) is familiarity and ease of programming.

Those Toshiba H-Bridges are working now in some traditionally-wired controllers and they haven't blown up yet. It would be very surprising if a train has not accidentally tried to cross the gap from one controller to another which has the opposite polarity. (Real train drivers would be fired for that sort of carelessness :slight_smile: )

...R

When looking for chips that produce several PWM outputs this Adafruit breakout for the PCA 9685 is the only practical thing I have found. If anyone is aware of any others please let me know.

The other chips I found are only available in surface-mount packages. I don't have the capability to use them and in any case if I were to attach any value to my time it would be much cheaper to use a Mega than to design, build and test a PCB.

...R

Why not just switch the leads from the PWM to each section of track?
That way, when the engine crosses the gap, it sees the same exact signal It's already using.
Or did I miss something?

boolrules:
Why not just switch the leads from the PWM to each section of track?
That way, when the engine crosses the gap, it sees the same exact signal It's already using.
Or did I miss something?

Are you thinking relays? It's more the old-fashioned way and I think Robin wants to get rid of that.

Jiggy-Ninja:
Not a model trainer, so I couldn't say. You would have to manually experiment with your locomotives to see how low the frequency can be and still have acceptable performance. I don't know how fast those motors can react. I probably wouldn't want to go lower than 100 Hz.

Were your calculations based on that? I have old designs (NE555 based) that advise around 100Hz. Lower will result in a humming engine, higher will result in too much heat; I guess you know that :wink:

Robin2:
When looking for chips that produce several PWM outputs this Adafruit breakout for the PCA 9685 is the only practical thing I have found. If anyone is aware of any others please let me know.

The other chips I found are only available in surface-mount packages. I don't have the capability to use them and in any case if I were to attach any value to my time it would be much cheaper to use a Mega than to design, build and test a PCB.

Robin2:
If bit-banging is not an option then I could use one or two Megas to get the required number of PWM outputs.

Remember that whatever you're using, you basically need to keep it in sync.

Robin2:
What I have in mind is an array that identifies which controller should power each track section (one element per section with a number 0-3). With the click of a mouse a controller can be linked to a section by writing the appropriate number into the array element for that section. There will be another array that identifies which PWM pin is associated with each section.

The sensible approach :wink:

Robin2:
What would you consider the maximum acceptable timing error where two outputs are shorted together by (say) a loco wheel?

It will usually not be one loco wheel. It will be all wheels that have the contacts (what do you call it in english, those little contact springs? Wipers?); and it's often all of them. So if the distance between the first and the last wheel is e.g. 20cm, at crawling speed it will result a very long time that the two sections are connected together.

I think you just need an insulated section of track just fractionally longer than the longest loco. Are all wheels electrically connected? It's probably just getting power from one pair of wheels. That means the insulating section can be very short.

boolrules:
Why not just switch the leads from the PWM to each section of track?
That way, when the engine crosses the gap, it sees the same exact signal It's already using.

That's how it works now. There is a bird's nest of wiring and relays and switches. I am hoping almost all of that can go.

MorganS:
I think you just need an insulated section of track just fractionally longer than the longest loco.

That is a non-starter for a few reasons. The main one is that it would need surgery to the scenic parts of the layout. But it would also mean that a short loco would be stranded. And there can enough problems getting power to the motor without reducing the number of wheels available to get power.

The controllers I already built are (I think) using the standard Arduino PWM frequency - 490Hz.

...R

Robin2:
That's how it works now. There is a bird's nest of wiring and relays and switches. I am hoping almost all of that can go.

It can and if enough others want the same then there's a good chance to introduce something new to the market already suggested, put the control in the engine cars and power the tracks.

Consider that the bigger the layout, the greater the savings in time and money.

You could have 2 engines on the same track section. With control over individual couplings (as opposed to the special track piece with the electromagnet) you could have one adding/removing cars from another without concerns of which track section has what on it.

There could be a nice job in it. Get a startup going and sell out to Lionel before they reverse engineer it.

Think of all the copper it would save!

GoForSmoke:
already suggested, put the control in the engine cars and power the tracks.

That misses the point. I want to propose something that will work without any modification to the locomotives so that everything that works with the existing system will work on the "new" system.

For my own trains I don't even want powered track - just battery powered radio control.

...R

It looks like my concern about synchronizing the PWM signals was unnecessary. When I tried some bog-standard analogWrite()s on my Mega all the pulses were perfectly in sync on my 'scope provided the PWM values for each pin were the same. For example analogWrite(110); to each pin being tested.

I guess it is possible that something in a real program might upset this and I will need to remember to check.

...R

Just wondering, do you know those led driver chips with the PWM outputs? They're like super-deluxe programmable shift registers? You might not need a Mega board at all.

GoForSmoke:
Just wondering, do you know those led driver chips with the PWM outputs?

Do you mean something like the link I posted in Reply #21?

If you have links to alternatives I would appreciate them as I have not been able to find them myself (other than as SMD chips).

...R

Futurlec has the TLC5940 in 28 pin PDIP for $3.20.

If that link comes up blank, this is where I clicked to see the datasheet.

Orders ship from China, their New Year is coming up but you might get in before that delay.

The DIP versions are listed as obsolete.

You might not need PWM if you update daisy-chained shift registers often enough. Even at lower SPI rates (bigger divider) to get longer bus wires, that should not be much of a problem. Regular 74HC595's are cheap, Yourduino (China store) price was 5/$1 last I saw. They are low current output but you will be using the output to switch FETs, right?

Best part, all of the signals would be in synch with the latch.

Thanks. Lots of food for thought.

Even if the "customer" is interested I may not actually want to buy anything for 6 months or more. And the difference between the price of a genuine Mega and the price of the cheapest alternative is not going to make or break the deal.

I have at least one 74HC595 somewhere. I might experiment with it.

...R

sterretje:
Were your calculations based on that? I have old designs (NE555 based) that advise around 100Hz. Lower will result in a humming engine, higher will result in too much heat; I guess you know that :wink:

Actually no I don't, i just pulled it out of my ass as a ballpark "minimum acceptable" figure.

It looks like my concern about synchronizing the PWM signals was unnecessary. When I tried some bog-standard analogWrite()s on my Mega all the pulses were perfectly in sync on my 'scope provided the PWM values for each pin were the same. For example analogWrite(110); to each pin being tested.

I guess it is possible that something in a real program might upset this and I will need to remember to check.

...R

There's a few things that would probably knock them out of sync:

  • Changing the prescalers.
  • Disabling timers with the Power Reduction Register.
  • Directly overwriting the timer value register.

Avoid those things and you should be fine.

The problem is that a Mega only has 16 PWM outputs. That's not enough to run 10 sections of track with 2 inputs each. Multiple micros are probably not going to be easy to sync between, not as easy as the PCA chip from Adafruit would be if you fed the chips with an external clock.

One solution is to run one PWM and one non-PWM to each H-bridge. Set the non-PWM to LOW and use analogWrite(speed) for one direction, and set the non-PWM HIGH and analogWrite(255-speed) for the other direction.

There is a slight problem with this setup. In the first direction, LOW-LOW is the off state and in the second it's HIGH-HIGH. For the H-bridge you selected these states are not identical. LOW-LOW puts the outputs to High-Z (stop mode in the table), and HIGH-HIGH makes them both LOW outputs (brake mode). Brake mode will be more forceful than stop mode, which may become significant at lower speeds. So there's a chance there that with this wiring the trains will act differently depending on which polarity is applied to the track. It also might not, you'd need to test that to see if it has a significant effect.

If it is a big deal, then the solution is to have a dual 2-to-1 multiplexer circuit before the H-bridge to act as a crossover. This would let you switch which input the PWM signal is applied to. All I found on Mouser were quad circuits, but it's not a big deal just not using half of them. Something like this 74HC157 is suitable for that. Note that you would not be able to use 1 of these for 2 sections, because all 4 selectors are controlled by one pin.

To summarize, here are what I would consider to be your good options:

  • External PWM generation chips like the Adafruit PCA board or the TI TLC chip.
  • A Mega, which may need the 74HC157 crossovers like I mentioned above.
  • If this is a common enough problem, a CPLD or FPGA could be programmed to perform the controller routing and possibly even the PWM generation all in one chip. This is exactly the kind of project that one would be good for. The learning curve would be really steep though.

I need to explore the possibility of syncing two different Arduinos.

One thought for making better use of the 16 PWMs is to use some AND or NAND gates to steer the signal to the appropriate input pin on the H-Bridge. The Mega has plenty of spare I/O pins to do the steering.

This is an interesting idea

One solution is to run one PWM and one non-PWM to each H-bridge. Set the non-PWM to LOW and use analogWrite(speed) for one direction, and set the non-PWM HIGH and analogWrite(255-speed) for the other direction.

The thing about all the helpful suggestions for using specialized chips is that the time it would take me to master them would not be justified by saving the cost of a Mega. This will be a one-off project.

...R

Robin2:
One thought for making better use of the 16 PWMs is to use some AND or NAND gates to steer the signal to the appropriate input pin on the H-Bridge. The Mega has plenty of spare I/O pins to do the steering.

I had that same thought too. In fact my last post had an entire paragraph discussing it, and linked to a chip that already had the AND and OR gates correctly arranged to do that. It would only take one extra pin per bridge.

Looks like you're leaning towards option #2 from my list. Run with it.

I need to explore the possibility of syncing two different Arduinos.

All the Timers in the AVRs have the option of using an external clock source, which you can use to keep the timers incrementing together. Something as simple as a 555 timer should be good enough, you wouldn't need the accuracy of something like a crystal oscillator. I don't recommend doing this on Timer0 if you don't want to interrupt millis() timing. I would follow this procedure.

  • Stop the external clock signal to the Timer inputs. A single AND gate is enough to do this.
  • Reset all the prescalers and write all the Timer value registers to 0.
  • Once all the controllers have signaled to the clock controller that they are done, re-enable the clock signal.

Jiggy-Ninja:
All the Timers in the AVRs have the option of using an external clock source,

Another interesting idea. Maybe I can take a clock signal from Arduino A and use it to operate the timers in Arduino B. I could happily "waste" part of a timer to do that.

I'll try to explore this tomorrow.

...R

Robin2:
Another interesting idea. Maybe I can take a clock signal from Arduino A and use it to operate the timers in Arduino B. I could happily "waste" part of a timer to do that.

I'll try to explore this tomorrow.

...R

Just remember that if you want them to stay synchronized, every Timer that you use to generate PWM must be driven by the same clock signal. If you use one timer on Arduino A to generate the clock and the rest for PWM, do not try to cut corners and clock the PWM timers from an internal source. Set them to an external timer source and physically wire the timer output to each timer input on the same chip, in addition to sending a wire out to Arduino B for its Timers.

That also means that if you are using one of the Timer's Output Capture modules to generate the clock signal, you cannot use the other OCP outputs as PWM outputs. You'll lose 2 or 3 PWM channels that way, not just 1.