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.

Free Day recap and blasted Android

This post will be a shadow of the one that I had originally composed using the WordPress for Android app that I installed last night. It had wit aplenty, but my phone decided that it had a better idea – and managed to lose every character. With that aside, let me get started.

I completed my children’s toy. I designed in about 6 games that managed to please the youngest through to eldest, and have only used around 10% of the program memory. I wasn’t affected too much by feature creep, and I’ve started looking at a NiMH charging circuit so I shouldn’t have to open the box to charge the batteries. I clearly have a lot to learn about toy design, as the power connector has broken. I used some 100 thou pitch header pins to act as the power connector, and I forgot to close the box after a late night adding the scoring functionality leading to two snapped pins. This wouldn’t be so bad apart from the fact they snapped inside the female header pins. I ran into another hitch when water got spilt on the toy. I had used some fibre washers (instead of nylon) and even once the spillage had been mopped up I still had a 7ohm short across my power rails.

Part of the design brief was to build something to teach my children about electronics in one form or another. Every time I meet up with my family, I get reminded about things I did when I was younger; including sticking coat hangers in plug sockets, taking TV’s apart, and electrocuting my little brother with my bedroom light. The response to escapades was receiving a 100-in-1 electronics workbench. Whilst this was a great idea, I didn’t learn anything from it. Not because I already knew how the circuits operated, but because I followed the instruction booklet until the circuit worked. Schools answer to electronics was Ohm’s Law and logic gates. It wasn’t until university that I began to lean how circuits worked. I don’t want this for my children. I don’t want my children to be part of the throw-away culture that I grew up during, and I don’t want them to have to put up with toys where batteries last a day because the designer had to save a penny on a power switch. I’ve designed power saving features into this toy; the PIC sleeps whenever it can, and everything sits behind a LDO voltage regulator with a shutdown pin. I’ve not worked out what the battery life should be, but they lasted a month before the pins snapped. I designed the case with a clear front, and tried to minimise the number of tracks on the bottom layer. The idea behind this was so that it was possible to see how everything connects together. Of course this led to embarrassment when the lack of sleep and a silkscreen meant that a couple of chips were soldered the wrong way round. This shouldn’t have been a problem apart from when I delaminated one of the pads and had to solder in a jumper wire.

Yes, I did say pads. This board was primarily surface mount. Despite working on PCB’s daily, I still consider myself to be a hobbyist and unlike a number of hobbyists I prefer surface mount components. I am fortunate to have access to a PCB cutting machine at work, and so UV light boxes and PCB pillar drills are a thing of the past. The board is easy enough for a child to manufacture, but I wouldn’t trust my 6 year old with hand soldering a 0.5mm pitch TQFP chip. All I have taught my children is that they can write firmware for toys. I decided to supplement this was some soldering on another product, just to hammer home that the can create usable things. Whilst growing up, my grandfather made an electronic die. I wanted to go beyond this, so foolishly bought an electronic die kit. This was only £6 from Maplins (similar to RadioShack), but it meant having the kit straight away. The circuit consisted of a PPL chip, binary counter, LEDs and a load of passive electronics. The PCB has a solder mask and so was relatively simple for said 6 year old to assemble under intense supervision. I was tempted to make a case for it, and include a power switch to make it a finished product – but I am not happy with the design.

I know hundreds, if not thousands, of people dislike using microcontrollers to flash LEDs. An electronic die is a flashy light project, and one that is clearly doesn’t need a microcontroller. Using PLL and binary counter chips give a pretty simple circuit, but uses 30 components – and will eat through the PP3 battery. I am planning on designing a replacement circuit using the PIC18F14K22 as the brains. I want to use the minimum number of components whilst retaining the feel of a die. This means I need to use 7 LEDs as die pips instead of a single 7 segment display. The remainder of my circuit will consist of a piezo-transducer and CR2032 coin cell. As I’m powering the circuit from a 3.0v cell, I should be able to ignore the requirement for current limiting resistors on the LEDs. Failing that, it should be possible to use PWM to help limit the current. A diode and resistor are needed to protect the PIC from the voltages generated by the piezo, and a resistor is needed to bias the MCLR pin. This gives a total of 13 components.

I have no experience of cost analysis, or cost reduction. It is immediately obvious that there are fewer components. This means a cheaper PCB as the PCB is smaller and requires less processing. Smaller PCB and fewer components mean cheaper shipping. Fewer components means less warehouse utilisation and the board is quicker to assemble. On the other side, the PIC costs slightly more than the PPL and binary counter chips, and time is spent programming the device. An additional overhead cost would be the time developing firmware for the die. I haven’t touched on the “running cost” of the die as battery life needs to be considered. And finally, there is the case of perceived value, “coolness”, and appropriateness.

Functionally, the bought kit performs adequately. Once the button is pressed, a 100nF capacitor is charged and used to trigger the PLL. As the voltage decays, the PLL slows down and eventually stops. The effect is that the LEDs flash as the numbers roll round and stop on a particular value. This value is effectively random, as the numbers roll round quicker than human perception. My version would use the PIC’s ADC to convert the piezo-transducer voltage to a binary representation. Additionally, I will keep the PIC in a low-power sleep mode to be awoken by the piezo voltage spike. My chosen PIC is a 20pin device, so I don’t need to resort to Charlie-plexing to light the LEDs. Both designs would have a suitably random roll rate, but in my opinion the PIC method has an overall higher perceived value.

I want this to look like a quality product. To this end, I am considering using iTeadStudio for PCBs and some of the tools at work for an acrylic case. We will see how that turns out, but for now I’ve got to wait for my Sparkfun freebies. I say freebies, but I’ve already paid form shipping, and I’m fairly sure I’ll need to stump money for VAT. Oh the joys of living in the UK.

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.

 

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.