# Stereotypical beginners electronics project pt2

I’ve got some LCM7215’s on order for a work project so I’ve got some downtime until they are due to arrive. I’m going to run through the LTSpice simulation process, and look at the alternative solutions available.

Assuming you haven’t already, you will need to download LTSpice. This is a free Spice simulator provided by Linear Technology. It’s pretty basic, but it has almost everything I needed to get off the ground. By using the following equations I managed to select starting values for my oscillator.

Using R = 255k, R1 = 1M, R2 = 255k, and C = 10u, we have a T of just over 11 seconds. Interestingly, when I simulated the circuit, my T was around 4.4 seconds. I am using fairly large values as I want to limit the current. The output is fairly constant, but there are a number of problems with my current circuit.

Firstly, I want  to use a single supply op-amp. This means I need to create a virtual ground or else the op-amp will be driven into an undefined state. The simplest way of acheiving this is my tying the inverting input to Vdd through a resistor equal to R1. Next, my output has a duty cycle of 50%. This is a problem as I want my device to be as low powered as possible. A reduction from 50% to 2% duty cycle would reduce my current consumption by a factor of 12. That is the battery would last 12 times longer. To adjust the duty cycle you need to include a diode in parallel withthe negative feedback loop. The idea is that the diode will all the capacitor to either charge or discharge quicker, depending on the polarity of the diode. As we want the capacitor to charge quicker we need to allow the current to flow to the capacitor quicker. Lastly, I have specced out the op-amp that I want to use. The LMC7215 is a low power single supply rail-to-rail comparitor that ticks all of my boxes. The only problem was that it is a National Semiconductor part, and LTSpice is Linear Technologies. This means that the part won’t already be contained in one of the built-in libraries.

To overcome this, National Semiconductor have released the SPICE definition for that part, and LTSpice allows you to include a SPICE directive. There are a number of options when it comes to using custom SPICE definitions, but I will only cover the one I used. I had already built up the circuit around a different op-amp, so to continue I needed to change the model. There isn’t a way to change the model, so instead you have to delete the current one and drop in the replacement. My replacement was based on the “opamp2” model. Once the base model is inserted you need to insert the SPICE directive. This is a button on the toolbar that opens a text box. Take your downloaded SPICE definition and copy the tesxt directly into the text box. Select “SPICE directive” from the radio buttons, and click OK. Next, right-click “opamp2” and type “LMC7215/NS” into the Value field. This is based on the SPICE line starting with “.SUBCKT” comment. Next, we need to tie together pin names and definitions. This is done by editing the “opamp2” symbol. When you right-click on a pin, a box opens allowing you to change the “Label” and the “Netlist order”. The “Label” is the pins name and the “Netlist order” is the order at which they are mentioned in the SPICE file. In this example we have

“.SUBCKT LMC7215/NS     3  2  6  4  5”

That means the non-inverting pin is called “3” and its netlist order is 1. The inverting pin is called “2” and its netlist order is 2, etc.  Once that is set up, you can run the simulation.

With the circuit designed and simulated it is time to lay out the PCB. Unfortunately for you, I use EasyPC at work, and as that’s what I will be using to lay the PCB out in. I have given you the tools to create your own flashing light project using an op-amp as the starting point. Next article I will be laying out the PCB, and designing an enclosure for it.

Before I go, I wanted to run through the other two options. The PIC is based around the PIC18LF14K22. This is a low power device, and I happen to have some left over from my electronic dice project (still waiting for PCBs). Not much to say here. I’ve connected up the header pins for the ISCP, and using a 560R resistor I’ve connected both pins of the LED to the PIC (anode to digital pin, cathode to analogue pin – diode “points” to the analogue pin). The 555 circuit used a 560k resistor, 15k resistor, and 10uF capacitor to give a period of around 4 seconds, and duty cycle of 97%. By reversing the diode and connecting to Vdd the LED flashes briefly for 105ms, and stays off for 4 seconds. Again, the 555 is just too power hungry, and the PIC is a bit of a waste of technology. I admit, I could use a PIC10 or PIC12 and get better usage, but I left assembly behind more than a few years ago and I’m not planning on looking back.

As luck would have it the LCM7215’s have just arrived on my desk. And yes, the 5 SOT-23 devices did come packaged in an A4 envelope.

# 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.

# Excuses

I made a promise that I would have some extra info on stepper motors, and clearly this isn’t it. I got very distracted with the parallel port work. Parallel port stuff used to be easy in Win98 and earlier, but got more difficult in XP. By more difficult I mean I had to install “inpout32.dll” and do some funky stuff in C#. But to use “inpout32.dll” you have to know the I/O start address, so I started blindly running towards what I thought the solution was.

To find out your start address you can simply head to Device Manager, open up the LPT tab, and click on Resources. For me, the I/O range starts at 0378. This is not the value you want, as it is the HEX representation of the address. As it turns out the integer address is 888. To save the user from going through these steps I had to dip into WMI and extract the information from there. Now there is no one place where you can tie together “LPT” and port address, so I had to connect them together using the PnPDeviceID. This took me ages to figure out as I had never used WMI before. Long story short, I have got the stepper turning controlled from the parallel port and also from a PIC from RS232.All I need to do is write the article, and generate some usable schematics.

However, I got distracted by designing something for my kids to play with. As an engineer I can’t help but be critical of a large number of children toys. I am not including things like Mindstorms, but I guess you get what you pay for. I want to show my children that electronics isn’t magic. They all use technology in one form or another, but I don’t want them to take it for granted. I want to show how a TV remote works, and how they can use something instead of the remote to change channels.

At the moment I have specced out some features, and it has already suffered from significant feature creep:

• Fit inside a dual gang back box (850x1470x35mm)
• Clear acrylic lid to show electronics
• Attempt to route everything on top layer
• 16 red/green LEDs
• 16 tactile switches
• 16×2 LCD display
• Rotary encoder
• Powered from 4 rechargeable batteries
• Small prototype area

I want to make sure this will grow as they do, and have features so it will entertain all of them (20months – 6 years). There is little doubt that it will remain interesting for all of 5 minutes, but that is no different from any of their toys. Currently I am deciding whether I should include any IR LEDs for controlling the TV, or  Sparkfun’s 3-axis gyroscope. I’ve even got a small touchscreen digitizer that could make its way inside. And of course there is always room for a little speaker.

Clearly, there is some more feature creep to go.

# 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.

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.