Adding the (Figurative) Spark of Life to Scribble
Last time, I covered the physical parts of the Scribble pen plotter. Today, I’m going to explain the electronics that make it move.
Finalizing the electronics is a crucial step to make the interesting stuff happen. No spark, no motion. Figuratively, of course. If your electronics are sparking, shut them down immediately.
Scribble needs to have a self-contained power and control system. This turns out to require quite a lot of intensive design work. Stepper motors require special driver circuitry. Solenoids are harder to select than you might expect. Buttons? Two ICs and their own special daughterboard.
All in all, Scribble’s electronics are spread over three PCBs, two of which I personally designed. I had to make some consequential design choices- and unlike the mechanics I actually still have my notes!
Part of the Scribble Pen Plotter Series:
Scribble Pen Plotter Electronics
Much like Scribble’s mechanics, the electronics were built up over time without a particularly coherent plan. Fortunately, PCB design kind of requires the electronics to be buttoned up before you actually get around to building them. I’d do a lot of things differently if I started over, but things worked out fine in the end.
Each part of Scribble’s electronics fits pretty neatly into one of four categories:
- Microcontroller– What drives Scribble in all it’s functions.
- Motion– Motors, solenoids, and their respective drivers.
- Interface– Sensors and the keypad.
- Power– I think you can figure this one out yourself.
I’ll leave you with the high-level schematics for reference. There are two; one for the controller, one for the front panel.
Microcontroller
Given Scribble has to have a CPU, it makes some sense to start there. I chose the ATmega 328 for the decisive reason of having several to hand. In retrospect, something slightly more powerful would be a better choice. Having some extra pins to work with would also be nice.
Assigning the pins is harder than it looks. There are 23 unassigned pins in the DIP-28 ATmega; I’m using all but two. These are overlaid with the built-in features of the ATmega, leading to some compromises. I tried to keep similar functions together wherever possible.
A big part of microcontroller based designs is figuring out which pins are available for GPIO versus one of the built-in peripherals you want to use.
Quite a few pins are taken up by those built-ins. The rest were selected on a compromise between software simplification and PCB simplification.
Port B has four PWM channels. I reserve one for each solenoid and one for the motors. I put the status LED here because it can’t go anywhere else. I’m using a crystal, so the oscillator pins are taken. One pin is left open because I don’t need it. No idea what that could be used for, but hey it’s there.
Port C has all the ADC channels, which I’m not using. I put all the interface signals here. Pin 1 is left open, because it’s also the reset pin. I’m not using it as such, but I don’t need the extra pin anyways, so meh. It’s also on the other side of the chip, which complicates trace routing.
Port D has the UART, so those two pins are reserved. The remaining six pins are used to drive the three motors.
As luck would have it, none of these signals are anything but a standard 5V logic signal. A few signals have series resistors but that’s all the special treatment needed.
Motion
Moving things requires a lot of energy- more than a microcontroller can provide. High power drivers are therefore required. I used stepper motors and solenoids, both of which have their own specific needs.
This is arguably the most complex part of the electronics. It’s definitely not as well-designed as it could have been. I used parts I already had to hand, which forced a few sub-optimal design choices. It will work though, and that’s what really matters.
Stepper Motors and Drivers
My motors are left over from a failed CNC project. They’re bipolar types in NEMA17 cases. I got them from Adafruit; they still sell the same motors. Powered up at 12V, they take 0.35A per phase. I expect them to run at 200RPM easy, though it’s hard to accurately predict.
Each motor requires two H-bridge circuits to drive them. Rather than try a discrete bridge, I used the L239D (also left over). These chips combine power transistors, level shifters, protection diodes, and logic in one package. L239D’s aren’t great, but they are more than adequate for this project.
As far as I can tell, these are completely generic 200 step motors in a NEMA17 case. No marks of any kind- not even a serial number.
There are other ways to drive a stepper motor, but I chose the simplest possible driver circuit.
Each half bridge is always in the opposite state of it’s pair. Because GPIO pins are at a premium I chose to add a 74HC04 hex inverter, halving the number of motor control signals.
Cooling is required for extended operation; my calculations show each driver dissipates 1.26W maximum. This is just within spec for convection cooling without a heatsink. I expect to use the enable input to drop the idle current, which means less heat when the machine isn’t working. Fan cooling ought to cover the rest.
Solenoids and Drivers
Originally I wanted to use the DSOL-0640-12E solenoid- chosen specifically for having quick connect terminals. As soon as I got them, things went sour. Notice I’m not linking to them- you don’t want to use them.
First, they aren’t as powerful as I expected them to be. Second, they used 4-40 screws which are a pain to find. But it’s the final problem that led me to put them away.
You see, they have QC terminals built-in, but the plastic holding them is incredibly brittle. I broke one off within seconds. Even the solenoid wire broke off. You can’t realistically remove the connectors as such. Which rather defeats the point of having a removable connector, doesn’t it?
If your product can’t stand up to basic use, you designed it wrong.
This is a solenoid. I have two, and they take all the work I give them.
Disappointed with the DSOL, I switched over to these chunkier types from Adafruit. At no point did bits of them break off under normal use. I shouldn’t have to say that, but here we are.
Bridging the gap between CPU and solenoid requires a simple switch type driver. A MOSFET is ideal. I chose the IRLZ14. Vth is 2V max, Ron is 0.2R @5V. Sounds just about perfect for this application. Thermally, they easily come under the threshold where a heatsink would be mandatory. Fan cooling covers the rest.
MOSFETs have effectively infinite input resistance, so driving them from the ATMega is not a problem. I added the series resistance for high frequency performance. The extra 10K to ground ensures the MOSFET stays off if the ATMega output goes high impedance.
Diodes are placed across the solenoids to clamp the inductance. This is crucial, not simply to protect the MOSFET, but to ensure the solenoid current remains continuous under PWM.
Interface
Plotters don’t work by themselves. You need some way to get data into the machine, either via another computer or manual buttons. There also need to be internal sensors to detect certain machine states. These are handled similarly to the other inputs, so I’m going to lump them all together.
Serial Port
Of all the possible communications protocols, the classic UART-based serial port is probably the easiest to implement. Most microcontrollers have a UART-built in, so the hardware cost is nothing. Serial ports on PC’s are almost entirely gone, but you can buy USB to serial converters cheap. UART chips are somewhat expensive, but still available. Really, I can’t think of anything better.
Serial communication is typically full duplex, with a dedicated channel for transmitting and receiving. You just hook the RX signal to the TX signal. A common ground is also provided bringing the total connections to three. Looking at the available connectors, I decided a standard 3.5mm TRS audio jack would work. Audio cables are dirt cheap and available.
As far as I can tell, this is a pretty universal standard. Careful though, the actual signal voltage is NOT standardized!
I decided against the proper RS-232 implementation using high voltage signals. Over short cable runs standard 5V CMOS signals should be sufficient. Buffers might be necessary for a long cable. The UART is simply connected to a 0.1″ header, so future signal conditioning is possible with an add-on board. Wireless support would be interesting…
Hardware flow control is not provided for. ATmegas don’t have the signals built in anyways. Instead software based XON/XOFF is used. Plotters are slow enough that this won’t be a problem.
Front Panel
Manual controls were a very late addition, added after I started seriously working on Scribble. I had to resort to using shift registers as input expanders to squeeze all the buttons in.
I count a minimum of 15 buttons: 4 directional buttons, 1 pen toggle, 1 reset, 1 halt, 8 pen select. I’m using 74HC165 8 bit shift registers, so I have to use two. For the most part this is just plugging in signals to the appropriate pins.
Buttons are standard tactile types. For the direction control I used a 4-way navigation switch, courtesy of Adafruit. 10k pull up resistors are provided for all contacts. Closing a switch therefore provides a logic zero, but the shift registers are wired up to invert that for ease of programming.
I threw a two-color LED on to show the current state. Green for idle, yellow for running, red for error. Tells you what’s going on at a glance.
For practical reasons, the manual controls are split from the main control board. A 10 wire ribbon cable connects it to the main control board.
Sensors
While it’s entirely practical to run a stepper motor open-loop, it’s also really handy to have some limit sensors for sanity checking. Ideally there’d be a sensor for each end of the carriage, paper, and an index on the pen carousel that doesn’t yet exist.
I decided on the EE-SX 672 photo interrupter for all the sensors. Being a simple optical device, I can use it for every sensor role. It has open collector output, detects light or dark, and works from a variety of voltages.
These all-in-one sensor blocks even have an LED to show you their current state. They even have the pin functions molded straight into the plastic.
Having separate signals for each sensor would be nice, but the ATMega pins are used up. Since these sensors have open collector output, I’m combining similar sensors (e.g. carriage left/right) one one line. Software has to keep track of the direction parts are moving when the sensor trips. Combining the outputs of sensors like this is a common trick.
However, installing the sensors requires some modifications to Scribble’s hardware. Since I can test Scribble without sensors, I’m going to delay installing them until after the first test. There are a few unsolved problems that need considering first.
Power
Last up is the power supply. I don’t fancy trying to build one from scratch, nor do I want to use an external plug pack. There’s plenty of prefab power supply modules to choose from.
The motors and solenoids run off 12V, while the rest uses 5V. Calculating the current drawn for each voltage is a simple set of additions. Most of these values come from the respective datasheets. Reasonable estimations fill in the rest.
12V Loads: | 5V Loads: |
---|---|
6x 0.35A per winding (0.7A per motor) | 1x 0.005A ATMega @ 8MHz |
2x 1A per solenoid | 2x 0.01A per LED |
5x 0.024A per driver | 5x 0.012A per sensor |
1x 0.1A for the fan | 3x 0.06A per driver |
4.32A Total | 0.265A Total |
Based on these numbers a reasonable power supply would put out 4.5A @12V, 0.5A@5V. I settled on the RPD-65C which is a near exact match: 4.5A @12V, 1.2A @5V. The extra current on the 5V rail seems excessive, but there’s not a better option. Consider it future proofing if I ever decide to add extra features later on.
Chunky, clunky, and a whole lot easier than the alternatives.
There’s still the matter of getting AC power into the power supply. I simply use a standard “power entry module”. They’re a touch pricey, but they cover all the basics: switch, fuse, removable cable, sometimes a filter or pilot light. The only high-voltage stuff you have to do is the internal cable.
PCB Design
Building the two circuit boards for Scribble is not entirely outside the realm of a protoboard, but I feel a proper PCB is more appropriate. I can repurpose both boards for other CNC/robotics projects so there’s no waste.
As far as my PCB design skills go, both were competent enough. There’s some lingering design issues involving physical clearance, but thankfully they aren’t so bad this time.
Control Board
Physical constraints are minimal for the control board. I put all the power connectors on two adjacent sides, and the signals on the other two. A tight squeeze in a few places, but it all fits together. I tried to keep the power stuff as separate from the logic stuff as much as possible. Ground planes fill almost all of the unused space.
All soldered up. No major mistakes here!
Unlike some boards I’ve designed, this one has no footprint errors! I did decide to flip the sensor connectors around, but they’re symmetrical so that doesn’t matter. It got tight in a few places, but I left enough space to squeeze everything in. Phew! I’d definitely go for a slightly larger board for a second design.
Front Panel Board
All the pen select buttons are grouped into a 4×2 grid. System control is a 1×3 group to one side. Movement is all concentrated in the navigation switch. The LED goes where it fits. Everything else can just go wherever. It’s not a particularly complex layout job.
Top side, with all the buttons and stuff. Note that I forgot to account for the height of the capacitors; bending them over slightly was the easiest solution.
Bottom side, showing the reverse mounted connector that may well have been more trouble than it was worth.
Notice how I put the cable socket on the underside of the board. When mounted, the top of the PCB is facing the front panel itself. Easier to put the connector on the side without sheet metal in the way.
Also notice how I didn’t leave enough space around the connector so I can’t use that mounting point. Because there appears to be a natural law that all of my PCBs must have at least one (1) egregious cock-up. The control board was an exception.
Installing the Electronics
Fitting the electronics requires a few modifications to the structure. Due to the overlapping development, I did most of these while finalizing Scribble’s mechanics.
Most of the installation work comes down to routing cables. There’s a dozen of them, and half of them must be installed to get Scribble working. The rest of the work is simply mounting things appropriately.
Control Board and Power Supply
Power and control fit snugly below the platen. This bit of the design, at least, never changed.
I simply tapped M3x05 holes directly into the plywood. Unlike some attempts at mounting electronics, I had no alignment issues this time around.
While not an ideal layout, there’s plenty of space to route the cables. Cooling air enters from the left side, passes through this central chamber, and exits on the right side.
Cables
Cabling is done in an ad-hoc manner. I used “Euroblock” connectors to allow some modularity. Given how hard it is to open Scribble up, this allows for repair/modification without stripping her completely naked- the indignity!
You can see my original plan was to put the blocks in a row on the bottom there. In practice there isn’t enough room. I later realized putting the block on the piece that the motor is attached to is a far better idea.
The actual cables aren’t too interesting, but here’s what the control board looks like with most of them hooked up.
I used MTA-100 connectors for the small connectors. The bigger ones use MTA-156 connectors. I find these connectors to be adequate for pretty much everything I’ve done. Ribbon cables were used for the bulk connections.
Line power is fed through using QC connectors, terminating in an MTA-156. Unfortunately, the connector on the RPD-65C is facing the wrong way. You know what? It doesn’t get in the way, so it doesn’t matter.
Keyed connectors are really inconsistent with which way they’re “supposed” to face. This isn’t the first time this has happened to me. This isn’t even the first time it happened to Scribble!
Meanwhile the other end is basic QC spade connectors. I left lots of cable to ensure the panel can be removed.
Wiring up the carriage is uniquely challenging because it moves around. Without some kind of support, it’s cable will get tangled up in the mechanism. Solutions are varied, but I used this idea from Hackaday since I already have a broken measuring tape.
At first glance, this looks great!
Moving the carriage reveals some… unintended behavior.
Functional, but I’m not that happy. I suspect the wire I used is just a little too stiff to bend that tightly. Good enough for a test, but I’m definitely going to have to replace this little hack job.
Front Panel
The front panel is screwed onto the aluminum face plate. 3D printed buttons give a more professional look to Scribble. Sadly most of the buttons don’t do anything right now, but they’re there when I need them.
I have much to learn about 3D printing, but this is a job it’s well suited for. Labels will come later.
Getting the panel together had it’s issues. There are exactly four ways the cable/connector can go together. Only one combination is correct- two at most.
Past me deserves some credit here. I manged to design the cable so it’s relatively insensitive to how the connector is laid out. Except for pins 9 and 10. Pin 9 is a signal; pin 10 is ground.
Since pin 10 is ground, I can’t just swap the signals at the ATmega. Nor can I cut the traces on the PCB because the header shroud is in the way. Things got messy, fast.
I learned these IDC connectors do not come apart in one piece. Also in casualty is the header I had to replace- it was installed the wrong way around.
I had this idea to flip the problematic wires around. Then I put the connector on backwards, ensuring the signals were still the wrong way around.
I then cut the cable apart, and soldered it back together again- but it was still wrong!
Turns out I had been testing the wrong wires the entire time!
Having figured out the correct pads to test, it was then trivial to get the connection right. Note the fan cable on the right. Also note the plastic shroud around the fan to direct the air.
Four attempts to get a single cable the right way around. That is… not a good ratio.
After a brief problem that turned out to be a partially crimped connector, the two boards finally passed a continuity test. Putting the connector on the bottom was a good idea, but my execution was terrible. I’m going to cut pin 10 of each remaining board and simply leave it floating for future projects.
Miscellaneous Loose Ends
I designed around three sensor groups, but as it currently stands they aren’t necessary to get Scribble off the ground. I don’t have the three-conductor cable to connect them anyways.
There’s a connector for the UART serial port, but the UART isn’t required for basic tests. Once again a lack of an appropriate cable makes me hold off.
The fan is a simple two-conductor affair that is not worth explaining in detail.
Finishing Up
Putting the electronics in Scribble is arguably the easiest part. Sure, there’s some tricky design stuff here and there. Functionally though, it consists of plugging together pre-made parts. I guess most electronics projects go that way, but it still feels a bit underwhelming.
Designing PCBs remains the hardest part. Not because of any major cock-ups this time, just cramming stuff into a small-ish space. If I had left just a little more space around the front panel connector it would be perfect. A few other connectors ended up tight.
Putting the front panel connector on the bottom of the PCB was a little too clever. Had I put a little more thought into things it would work perfect. But the extra ground wire screws everything up. Running a separate power cable would solve this issue.
Solenoids are surprisingly hard to work with. Their datasheets are confusing, and the given values aren’t as useful as it feels like they should be. Not to mention they’re hideously expensive. I’m genuinely considering building my own from now on.
If I had to redo the control board from scratch, I’d replace the L293Ds with something like the TMC2590. These are far more modern parts that do a lot more than the L293D ever could. Amazingly, they’re less than half the cost despite being far more capable.
Cabling is a crucial thing to consider, yet easy to overlook. Power and logic signals don’t mix well together. I deliberately kept the logic cabling away from the power stuff. I don’t expect to need extra shielding, but when you mix big power pulses with low-level logic you need to consider the potential for noise coupling.
What’s Next?
Having done a lot of work installing various electronic components, you’d expect some kind of test to cap things off. I can’t do that. Not yet.
Any tests more advanced than “poke it with the mutlimeter” require software support. Software- even “trivial” software- is highly complex. It’s going to have to be developed and tested in stages. The first stage is partially written, but until the electronics were installed, I couldn’t test it. Even though this is the most basic, least complicated possible software it’s still complex enough to justify it’s own article.
While I’ve been installing the electronics, several mechanical flaws have been revealed. Nothing too serious, but I have to do a few minor tweaks before a proper test can be carried out.
In the interim, I’ve been working on the pen change mechanism alongside everything else. My design is maturing, and I’m pretty sure I’ve finally got something that will work.
Despite all the setbacks, Scribble is still progressing at a steady pace. I should have her working by April 2023. May at the latest.
Projects have ups, downs, and really long level bits where not a lot of interesting stuff goes by. The electronics were done long before Scribble was even called Scribble. Writing this article was like taking the bus across town. Not particularly interesting unto itself, but necessary to get to the interesting stuff.
Next month I’ll do my best to get Scribble up and moving. It’s pretty exciting! All those years of work, and I’m finally going to see some results!
Have a question? Comment? Insight? Post below!