Category Archives: Tutorial

Bad day at work? – How I destroyed a £2300 piece of equipment

It’s been longer than I care to admit since my last post, but sit comfortably because this is a tale worth telling. It is about all the planning and thought in the world can be let down with one careless oversight.

We use a fairly inexpensive HiPot tester (Clare H101) at work to check that some passive circuits are isolated from each other. This involves putting 850V on one circuit and checking that the hipotleakage to an adjacent circuit is less that 5mA. We currently use a custom switch box to manually dial between tests and perform the hipot test, remembering which combination of tests fail to determine which circuits require rework. This is a fairly quick (40 second) test but relies on the operator to connect the unit under test (UUT) correctly, dial through the tests correctly, record the results correctly, and stamp the correct section of the associated paperwork. With all the workplace distractions it is easy to forget (or overlook) one of those steps. Granted, the operator is working with equipment that has the potential (pun intended) to kill someone, so you’d be forgiven for thinking that additional care must be taken. But with all things, complacency settles in pretty quickly.

So a switch box was designed and software was written to control the hipot tester (using an partially documented protocol) and switch between circuits. This would check for the presence of the UUT (although various constraints prevented continuity checks of the individual circuits), and only proceed with the test in the UUT was connected. The HAL 101 included a guard circuit that h101is designed as a dead-man’s switch, but ours was fitted with a wire link instead.

The guard circuit calls for a no-volt switch to be used, whereby the test would only start if the contacts were joined. The connector was physically located with Mains parts (IEC inlet, fuses, 230/120V selector) and the connector was rated to 230V with L and N labels on the screw posts, but there was no mention of what voltage the guard circuit operated on.

It was decided that it would be safer if the hipot tester couldn’t initiate a test without software control, and that breaking the guard circuit would achieve this goal. The circuit was designed with track separation for 230V and a 230V relay was spec’ed for use with the guard circuit. All testing was carried out by bypassing the guard circuit until confirmation was received from the manufacturer of the working voltage of the guard circuit. This was received this morning, and all wiring/connectors/etc. needed to be rated to at least 5V 20mA. Perfect! The relay and wiring were completely overspec’ed but it meant that I could use a panel mount 3-pole 3.5m TRS connector instead of a large 230V rated connector. The wiring was finalised, and everything was soldered, connected, and screwed together for the final test.

The first test went through alright, but then the communications to the hipot tester went down. Maybe there’d been a software issue after all. Hardware and software were restarted but the comms were still down. Time to crack out RealTerm as an ASCII protocol had been used. Still nothing. Maybe the comms settings had been changed or corrupted, but everything checked out. What followed was an EE’s (almost) worst nightmare – the smell of magic smoke. Oh dear. Something going wrong is completely manageable; you can examine everything, evaluate possible failure modes, determine what was the cause and propose a fix. But what magic relaysmoke does is alert everyone in the room that you have messed up. Everything was quickly powered off and unplugged, to the sound of cheering from around the office. The only thing that had been changed was connecting the guard circuit so that seemed like a good place to start. Even if there was a solder defect or etching problem on the board then the worst thing was that the relay contacts were shorted together, which wouldn’t cause magic smoke. The connectors were taken apart and all the wiring was checked. Everything seemed alright. Time to take the hipot tester apart. The hipot tester was now already broken, so the ‘VOID if removed’ sticker wasn’t going to stop me.

The Clare H101 is available for around £2300, but accidents happen and I was outside of my probation period so I wasn’t fearful for my job. Opening the hipot tester revealed 2 screws rolling around the case. Maybe it was my lucky day, maybe it was just a coincidence that I plugging something in for the first time at the same time nte0505mcas it went bang. Unlikely… but possible. It didn’t take long to discover a slightly charred and cracked isolated DC-DC converter that powered the external interfaces (remote buttons, lights, beacon, serial interface, and guard circuit). I didn’t really want to send a unit back for a £300 fixing charge when a £5 component had failed (rest assured that my colleagues also picked up on my re-framing of “I’ve blown up a £2300 bit of kit” to “this £5 component has failed”).  But what caused it to fail?

I looked over everything again. The connectors had no stray bits of wire, the soldering was perfect, the relay contacts were switching like they should, the COMMON terminal was connected to 0V… WHAT?! Why is that connected to 0V. I opened the schematics and PCB artwork, the relay was only connected to a 5.08mm pitch connector. There was no way that this relay could be attached to 0V. I’d even checked this before and there were no shorts then. What else had I changed? Something must be different. And then it occurred to me, I had added an Earth bonding wire between the front and rear panels. My panel mount 3-pole 3.5m TRS earthconnector also happened to be metal, and so had shorted the sleeve (what I had designated common on the relay) to ground. Obviously when the relay switched across to close the guard circuit I had inadvertently shorted the isolated 5V of the hipot tester to ground (with the isolated 0V connected to the PC through the comms cable). The isolated power supply did not like this, and promptly died. I held my hands up to this. I had even added a cable gland to not use the TRS connector but decided against it at the last minute.

This is where it pays to understand the system as a whole. Yes, I was the only engineer to work on this and so I should’ve known better. What this meant was the avoidance of the fruitless exercise of software engineers blaming electronic engineers blaming mechanical engineers etc – I had to work with myself to ignore blame and work out what and why something had gone wrong. It was my fault that the Clare 5V was shorted to ground but I would learn from that mistake and make sure that it wouldn’t happen again. What actually happened was that I blamed Clare for not designing a more protected interface.

I don’t have access to any circuit diagram, but it is clear that the guard circuit did not include sufficient protection. Any inputs from the outside world should limit the voltage and current (as much as possible) before interfacing with anything sensitive like a micro-controller or logic gate. I tend to use the following circuit.input-protection

This limits the voltage and current to the gate of a MOSFET where I can then have voltage level conversion to my micro-controller VDD. This is by means not the only method, and other people may have other ideas, but it is a good place to start. However, having this alone will not protect against what actually happened, and that is that the voltage out drew too much current that the regulator burned out. Again, there are many ways of preventing this. As a starting point I would use a regulator that had over-current protection or thermal cutout. The hipot tester used a Murata NE0505MC for around £4.80 in 1000’s. A cursory check has turned up a BurrBrown part, DCP010505BP, for only £1 more. This features thermal cutout that would prevent the component failure. However, this is only part of it. What happened if the guard circuit was connected to something outputting 24V (like a light gate), or accidentally shorted to ground? Again, then the output should be current limited (using a resistor or PTC fuse) along with diodes to clamp the voltage. This obviously wouldn’t protect against connecting the circuit to mains voltage but it is a start.

If you have read this far then please take two things from this. Firstly, if you are interfacing with the outside world then please use protection. Protect what is going out and what is going in. You don’t need to go overboard, but if there is a chance that something will get shorted to ground or a power rail then limit that current. If you are powering with a DC socket, then include over-voltage and reverse polarity protection. A diode, resistor, or MOSFET are a lot easier and cheaper to replace than every IC on the board. Secondly, if you are the outside world, do not assume that the other designer has read this. Before plugging something in, check, check, and check again. If you are connecting to something that says it requires no-volt connection then don’t short it to a rail, just provide a relay. Obviously I could’ve taken the 5V into my circuit and then supplied my own 5V output, but in this case a relay was supposed to be safer as I may not have had the same 0V reference. Even though you are sure, check continuity between the relay contacts and any current source or sink – that means your voltage rails, case, ground, any IO etc. Read the manual and email the manufacturer for clarification. If something smells hot then be prepared to switch it off quickly. Limit current if you can. The manufacturer said that the wiring had to be capable of withstanding 5V 20mA so I could’ve included a resistor to limit that current. Would it have saved the isolated DC-DC converter? It’s tough to say, but it might have dragged the voltage down enough to affect communications and point to a potential issue.

I hope this has been informative and/or entertaining. To finish the story, my boss had a good laugh at my expense, we chalked it down to a learning experience and a replacement DC-DC converter is on order for me fit. It’s great being a double-E.

Stereotypical beginners electronics project

The In-Laws have just moved house. I say move house, but what I mean is sold their old self-build, bought a field in deepest darkest Norfolk and are planning to self-build again. This is great news because it means they’ve got a blank canvas to fill with numerous eco-build projects. The first of which is a criminal deterent. I’m sure the working ip-camera will be used at some point but until then a decoy camera will have to suffice. Decoy cameras work fine so long at they remain believable, and so this decoy camera needs that little extra bit of polish; a flashing LED.

The flashing LED project is the “Hello World” for those whose programming language is solder. Any time you learn a new microcontroller, you will face the flashing LED tutorial. This is designed so that you can set up all the important registers and interface with the outside world. When you talk about flashing LED projects without using a mcu, people will generally offer the 555 timer as a solution. I’m going to look at three options, and hopefully have a working flashing light at the end.

The main design considerations are the need for a long battery life, and a fairly cheap design. I’m going to be using the LPKF Protomat C60 at work to cut the board out, and I’ll be trying to use components I have in my bits box, or can get free samples of. I am aiming for the design to fit on the back of my selected battery solution, and have a suitable mounting fixture integrated into the PCB design.

I know I need to use battery power, and once installed the batteries can’t really be changed. The build should take 2 years, and so I am aiming for more than that. I’m going to overengineer this aspect horendiously as my professional integrety is at stake. Obviously, a car battery would be a rediculous solution, and although space is not at a premium I’d still like to make the package small. My options consist of a single PP3 battery, a single CR2032, or a pair of AA batteries. The CR2032 only has a capacity of around 200mAh, with the PP3 only doubling that. The AA has to be the victor at well over 1000mAh. Thankfully, the AA battery holder is a also almost half the cost of a CR2032 holder. Lastly, is the actual flashing LED. A flashing light project without a flashing light simply isn’t. The minimum current draw I can reasonably expect to look at is 1mA. Any lower and I dont think the LED would be bright enough to see at a distance. The human visual cortex holds onto an image for about 1/15s, or approx 66ms, so I think that 100ms is a reasonable on time for the circuit.

I’ve mentioned the 555 timer as a possible solution so lets examine that a bit closer. Normally, the 555 doesn’t output a duty cycle of less that 50% which is bad for a low power design. The duty cycle issue can be addressed with a 1N4148 in parallel with the resistor between the discharge and trigger pins. Alternatively, you can reverse the LED and connect to Vdd instead of Vss, sinking current through the 555. The ICM7555 is Maxim’s low power derivative of the 555, having a typical supply current of 60uA. Assuming a battery capacity of 1000mAh, the 555 would run for 16666.67 hours (a month less than 2 years) without flashing an LED. The 555 just doesn’t cut it.

The next obvious solution would be a low power microcontroller. This is great as the LED can do more than just flash. It can reverse bias the LED to work as a light detection circuit, and the LED can do a double flash for extra realism. The PIC18LF13K22 has a low power sleep current consumption of only 460nA. This would be 34nA but I need to keep a timer running to wake the thing up. Even a draw of 460nA  gives a run time of 248 years on a 1000mA battery, which is just crazy. Adding the LED gives a battery life of 5 and a half years. With a BOM of only 5 components this ticks every box, but using a microcontroller feels like cheating. I think this will be my backup plan if the next idea falls through.

My final option is an op-amp. The op-amp in question is Linear Technology’s LMC7215. By using positive and negative feedback and a crafty 1N4148, the LMC7215 can be made to oscillate with a definable peroid and duty cycle. I used LTSpice to simulate the actual circuit and can extract the predicted current draw (tutorial to follow), giving a battery life of just over 5 years. Using my parts box, I managed to get a on-time of 92ms, and a period of 4 seconds.









The op-amp circuit does give a higher parts count, but this still ticks every box, and I think it is more elegant than the PIC solution. Next article, I will be showing how to use LTSpice to come up with the appropriate values, and importing SPICE definitions into the circuit. I will also lay out the PCB in CAD, and start the enclosure design. See you then.

Stepper Motors Part 2

Hopefully some of you are still with me, and you haven’t blown up your supply, switching device or coil.

Before we continue I’d like to ask a question; what current can your supply handle? Let’s take a brief look at Ohm’s Law, or at least the mathematical equation describing Ohm’s Law. The equation V=IR is incredibly important in electronics, and so you should have it burned into the back of your skull. Assuming a 5V supply (not uncommon), and a coil resistance of 1.6ohms (again, not unheard of) – this gives a current of 3.125 Amps PER COIL. Please also bear in mind that by using the Full Stepping method we would be drawing 6.25 Amps and you could be facing some current issues. I was using an old PC power supply as they have reasonably hefty current ratings, and although the 5V rail was fine, running off the 12V kept tripping the thermal fuse when trying to switch just a single coil.

Stepping a single step is easy enough, but you will invariably try to spin your stepper fast. You will hit a certain point where the stepper just stalls and lets out a whine (corresponding to the switching frequency). This is not necessarily a limitation of the motor, but of your driver circuitry. You have reached a point where you can’t energise the coils quickly enough, but there is a solution. By increasing the drive voltage it is possible to decrease the time taken for the current to saturate the coil, and so increase your switching speed. We have already ascertained that an increased voltage leads to an increased current and a blown supply, so we must find a way to increase voltage and keep current the same. There are a number of ways to do this, but I am going to cover the two simplest; linear resistive current limiting, and a chopper drive circuit.

Linear resistive current limiting is far simpler than it sounds. In fact, it is just adding a resistor in series with each coil. Similar to a current limiting resistor when driving an LED, this resistor limits the current going through the coil. Most motor manufacturers actively encouraged this technique up to the mid-80’s, but it has fallen out of fashion – either way this is probably the easiest and cheapest method available. The major disadvantage of this method is that the current is getting dumped in this resistor, and so the resistor will get HOT. This is NOT a job for the 1/4W resistor.

The chopper drive circuit uses PWM to limit the current in the coil. Either this can be open-loop control where the electronics has no knowledge of the current levels, and the duty cycle is determined either  through experimentation or calculation. Alternatively, closed-loop control can be implemented through the use of more complex electronics measuring coil current. The disadvantage of this method is based purely around the PWM frequency. Higher frequencies will cause RF issues, and lower frequencies (i.e. audio frequencies) the motor will literally “squeel”.

Next article I will be dealing with actual schematics, and including some code used to step the motor using a PIC18F.

Introduction to Stepper Motors

If you are reading this, then you probably want to know more about stepper motors. I will start at the basics and work my way up. I am going to be referring to the permanent magnet 2 phase variety. Other stepper motors exist but are beyond the scope of this article. A stepper motor is a brushless DC motor that is made to turn a fraction of a turn at a time. This means that the stepper motor is ideal for use in CNC machines (like the RepRap, Makerbot, Ultimaker etc) as the small steps – hence the name – allow for greater precision that is possible with standard DC motors. That is to say that the stepper motor can be controlled using an open-loop controller, as the step size is more or less constant and the motor shouldn’t slip – assuming the stepper is driven correctly.

If you have bought the stepper motor then chances are you already know quite a bit about its characteristics. If not there are some simple calculations and experiments to specify its performance.


Identifying Your Stepper

Not only do stepper motors come in a variety of shapes and sizes, they also have different coil configurations. This can help identify how to drive the stepper motor, and what sort of controller you are likely to need to drive the motor. Stepper motors typically have 4,5,6 or 8 wires, and these can be used to identify the type of motor. There are two types of stepper; Unipolar and Bipolar. This refers to how each coil is driven. A unipolar stepper only requires each coil to be driven in one direction, whereas a bipolar requires driving in both directions. Unipolar motors achieve this by each coil having a centre tap. Using a DMM, test each combination of wires. Each coil should have similar DC resistances, so this is a simple way of testing coil integrity. By working your way through the wire combinations it is possible to identify each coil (equal resistances), and if a centre-tap is used (half the resistance of a full coil). Generally a 4 wire stepper is bipolar and anything greater is unipolar.


Does It Work?

So you have left your DMM at home but want to test the stepper motor anyway. A very simple check of the coil integrity is to short either end of the coil together and try to turn the shaft. I wont go into Lenz’s Law, but basically by shorting the coil together you are apposing the generation of an EMF, and by extension the stepper will be harder to turn.


Characterising the Stepper

Firstly, you might want to know the resolution of the stepper. This is measured in degrees per step, with typical values ranging from 1.8deg to 7.5deg (or 200 to 48 steps per complete revolution). Obviously a smaller step size is preferable as it allows for greater resolution. Shorting out the coil, and counting the amount of times the stepper stalls when turning manually will give a value half that of the total number of steps in a complete revolution. Another method would be to build a controller and count the number of pulses needed to perform one rotation – however this requires a driver circuit.

The following are rules-of-thumb, and should be treated as such. There is every possibility that you can burn out the stepper coil, but you should be alright if you keep a close eye.

Coil inductance is useful to know as this dictates the maximum voltage the coil is likely to take. This can be measured by putting the coil in series with a known resistor (typically 100R to 1K), and exciting the circuit with a function generator (between 2kHz and 10kHz). Using the frequency, known resistor value, coil DC resistance, function generator output voltage, and the voltage , it is possible to calculate the coils reactance and thus inductance.

Maximum voltage is then 1000*sqrt(L). One of the motors I used these equations on gave a coil inductance of 0.00338H, and so a maximum voltage of 58volts. Given the coil resistance was only 1.6ohms, I would need 36A per phase, or 72A in total. This would obviously give me a very hot motor, and probably burn the coils out.

Most stepper motors are built to withstand an 80°C rise in temperature. Personally, I would reduce the voltage or current such that the stepper did not get that hot.

Another important bit of information you might want to know is the holding torque. This is the ability of the motor to remain stationary when under an external load. The following experiment might well leave you feeling like a school child but bear with me as communities responsible for open source 3d printers specify particular holding torque values. First you energise the coils in one of the modes of operation mentioned later, and then you suspend a weight from a arm of a particular length. The RepRap recommends a 13.7N-cm stepper, which corresponds to roughly 1.4kg suspended on an arm 1cm long. That is to say if your motor doesn’t move when loaded with a 1.4kg weight on an arm 1cm long then it has a holding torque of at least 13.7N-cm.

Modes of Operation

There are three simple ways of driving a stepper motor; full step, half step and wave drive. A forth method called micro-stepping exists, but this is beyond the scope of this article, and has the disadvantage of having unpredictable and unrepeatable step sizes.

  • Full stepping is when two coils are always energised at the same time. This allows the maximum torque of the motor to be used, at the expense of more current.
  • Half stepping is achieved by alternating between energising a single coil and two coils at the same time. This increases the angular resolution, but the torque fluctuates between utilising a single coil and both coils.
  • Wave driving is the least useful as only one coil is on at any one time. Although this allows less current to be used, it also produces the least amount of torque.


Stepper Circuits

With bipolar motors you have to create an H-bridge circuit. The H-bridge consists of 4 transistors (2 NPN and 2 PNP) switched in pairs (Q1 and Q3, Q2 and Q4) such that current can flow through the coil. H-bridges are used in the control on normal DC motors to achieve forward and reverse movement. Fly-back diodes are included to protect the transistors from inductive kick-back; that is the inductive spike generated when switching the coil off. As we have two coils, then 2 of these circuits will be needed. It might also be necessary to use Darlington pairs instead of transistors purely because of the current load. One extra thing to be aware of is that if Q1 and Q4 are on at the same time (due to the switching characteristics of the transistor) then you will be shorting out the power rails. Optimisations can be made to this circuit by switching Q1 and Q3 with an additional transistor as this will half the amount of IO needed.

Unipolar motors can be driven with a single transistor for each coil. Again, fly-back diodes are included to protect the transistors. Clearly this is a much simpler driver circuit, but can only be used for unipolar motors. This simple circuit has its own trade-offs; only half the coil is energised at any one time, and as such the amount of torque generated is less. I prefer to ignore the centre tap, and drive the unipolar motor as a bipolar motor. Granted the circuit contains a greater number of components, but you get more torque and ultimately the circuit differences are forgotten when working in software.


Driving the Stepper

At this point I have to leave you. This article is getting longer that I had hoped for, and so stay tuned for some information on the actual drive circuitry needed to efficiently (and not so efficiently) get your steppers moving. If you are desperate to get started then feel free to breadboard up those circuits already shown, but be wary of the current capacity of the switching devices and your power supply.