On our company's Christmas celebration (December 2013) all employees got a small present: A Raspberry Pi. This was the initial event for me to investigate what (internet of) things can be done driven by this tiny computer.

This blog is to document my findings and to share what others shared with me.

Dienstag, 13. Mai 2014

Building PCBs - Part 2: Two sided PCBs

In my last post I used the toner transfer method to build a PCB. Sometimes the arrangement of nets of a layout get pretty complicated or impossible to figure out. Even auto-routing fails or results in arrangements hard to work with.
In this case it might be better to use a two sided layout: Whenever a net cannot reach its target you can use vias to switch to the other side and go on easily. For the H-bridge IC I use to run motors (see Control motors - Part 4: H-Bridge) I need a two sided PCB so let's see how this can be done using the toner transfer method.

Two sided PCB
First of all you need PDF-files of the mirrored layout of the top and bottom layer. They have to be printed to toner transfer paper. Afterwards I punched the 3mm holes at the edges of the layout which are needed to screw the board into a box. Now I put one layout on the copper and used a permanent marker to amplify the holes made before. After this you can remove the layout and you will see the positions for drilling those holes. The next step is to drill the holes.

Now you have two layouts printed to toner transfer paper and the board - all parts with holes at the same position. After cleaning the board the next step is to iron the layouts onto the board. Now I arrange the first paper using the holes: they have to match exactly. Next I put the iron on the stack for some seconds to make the paper stick on the copper. After this the board can be flipped to repeat the procedure for the second side using the remaining print. If both papers stick well you can flip the board several times to ensure each side is ironed as explained in my last post. The rest of the story is the same as for single sided PCBs: Peel off the media, etch the board and remove the residing toner.

The upper side of the board after toner transfer...
...and the lower side.
(the dark areas are minor defects I fixed using permanent marker)
After etching the board you can drill the holes needed for common elements or vias. Do not drill through the board. Sometimes the position of the layout is not as exact as it should be and drilling through could break nets of the second side. I drill from both sides to the middle of the board - similar to  digging a tunnel. If the hole does not match exactly it does not matter. The elements can be soldered even if the hole is displaced a little bit.

After etching and drilling you can see how exact your work was. I'm satisfied :-)
First note: If you prefer transparent film you can orientate the layout as shown in this post: http://www.instructables.com/id/Two-sided-PCB-using-toner-method/.

Second note: You might asked yourself which kind of vias I use. None! I build vias by soldering strand on both sides of the board. Before doing this I put the board on two stencils. This builds a distance which helps to ensure that both sides can be soldered well. After soldering the excrescent strand can be cut off.

Dienstag, 6. Mai 2014

Building PCBs

A lot of things happened since my last post! I got a lot of new modules needed by my mowing robot (RF, distance sensors, 3-axis compass, charging IC, and so on). I have to investigate them before I can use them. This will be very interesting and a lot of fun ;-)
Unfortunately my breadboards are full and I have to bring all the stuff to circuit boards. So the next step is to learn how to produce those boards.

First of all you have to design your board. There is a lot of free software available. I tested Fritzing and Eagle and both do a good job. The disadvantage of Fritzing is it's limited elements-library and the disadvantage of Eagle is it's complexity since this is a professional tool. So I will use Fritzing for simply boards and Eagle for complex ones.
Both programs offer to order boards for professional production. This is pretty expensive although the boards offered by Fritzing and Eagle are low-cost-boards. So what is the solution? DIY!

Usually the nets of circuit boards form by etching copper of non-conducting areas. But how to "tell" where copper has to be removed and where not? Professional boards are created by using light exposition: the copper is hidden by a layer of light sensitive paint, the inverted board will be printed on a transparent film used to expose the board and acid will remove those exposed areas (and the nets resist).
This also can be done at home but at home it is not that easy:
  • The exposition can get blurred due to shadows if the film is not fully plain.
  • You see errors and mistakes not until the board is etched. So erroneous boards only can be trashed.
  • Boards coated with light sensitive paint are much more expensive then boards without that layer.
So DIY guys figured out another way: The toner transfer method. This technique is taken from bringing images to T-shirts or cups. The layout is printed to some sort of media using a laser printer and afterwards pressed under heat onto the target. The toner melts and goes to the target. This works best if the target's surface binds the toner better then the transfer media does. For T-shirts and cups special transfer paper is available.

For copper boards it is a little bit more complicated but not much. Most guys out there use any sort of glossy advertising media. After printing (mirrored) it has to be ironed onto the board. Afterwards it has to be put into water (for about 10 minutes) so the media can be rubbed off. There is no instruction which media works best - it depends on your laser printer. Some media is too thin and will not work. Some media binds the toner too much and paper cannot be removed completely. Additionally you put paper into your printer that isn't meant to be used in a laser printer and may damage the printer.
Instead of advertising paper transparent film for overhead projectors can be used. This works good but not reliable good. So at the end I managed to get this special toner transfer paper (used for cups) which works best for me.
Anyway: you have to find out what works best for you. This may be a little bit frustrating but you can be sure that there is a way for every laser printer.

How it works:

  1. Clean your board

    This is essentially for the binding of the toner. Any bit of dust will lift the paper and the toner won't bind! Any fingerprint brings tiny bits of oil onto the board and the toner won't bind! So clean the board precisely and keep it clean.

    I do a three-step cleaning. First, I use stove cleaner and scrub the copper in small circles. After this the board has to be washed and dried. Second, I use steel wool to polish the copper. Third, I use Acetone to remove any oil. It seems to be essentially to use a clean board otherwise the toner does not bind well.
     
  2. Prepare the transfer media

    I use that special transfer paper. In my case there are four things to take care of:

    First, print the layout mirrored and tell your printer to use as much toner as possible. Second, toner transfer paper is very soft. I can see patterns of the laser printer's transport-wheels on the paper after printing. These patterns decrease the quality of the printed layout in this regions so you should avoid printing in this regions by choosing a proper position. If the layout is too big to avoid this then try to reduce the number of SMD-elements or other small things in this regions. Third, the paper cannot be reused so print the layout as many times as possible on one sheet. Maybe your first try fails and you need another print for the second take. And fourth, avoid touching the transfer media to reduce oil of your fingerprints.
     
  3. Iron the layout onto the board

    Put the transfer media on the board with the layout down to the copper. Put some sort of cloth on it. Preheat your iron. The toner starts to melt at approximately 180°C. This is usually the setting between cotton and linen. Which temperature is properly also depends on the thickness of the transfer media and the cloth. Put your iron onto the stack and wait 5 seconds. Now the layout sticks at the current position and you can start to move around. The bottom of the iron is not hot equally so you also have to turn the iron around. Put some pressure on the stack. How much depends on the thickness of the transfer media and the cloth. Thicker means more pressure. The whole layout should contact the copper equally over the time of ironing. You should stop ironing if all regions of the layout are at 200°C. You might ask when this is achieved and I have no answer for you. You will see whether it was too hot, too long or too much pressure if you remove the transfer media. If so the toner has melt to blobs, especially at bigger areas of toner, and the borders are not clear.

    One trick I use is to vary only one aspect: I always use a timer to count the time of ironing and stop always at the same time. On different tries I only change heat or the pressure. This should help to figure out what your personal best settings are.
     
  4. Remove the transfer media

    Wait for cooling down the board and peel off the transfer media. If your media is transparent film you can use some ice because the toner and the media decreases differently on cooling down rapidly and the toner will lift off from the film almost completely.

    Review your board carefully! Some failures can be corrected by using a permanent marker. Take care that all nets are separated to avoid shortens. If there are nets together (for example near SMD-sockets) you can scrub the toner away by using the spot of a sharp knife.

    If the result isn't satisfying then try again. Remove the toner using Aceton and start at step 1. It is better to repeat the process then trashing a board after etching it. It took me several approaches the get a useful result! So don't be inpatient.
     
  5. Etching the board

    This is quite easy. I use Natrium-Persulfat. It is easy to handle and easy to store. Some acids are gassing so you cannot close the canister completely which means you have to store it outdoor since the gas is poisonous. Natrium-Persulfat can be stored in a bottle made of glass or plastic. But keep it away from children - it is an acid!

    The only disadvantage of Natrium-Persulfat is that it works best at 50°C. So I put the liquid into microwave oven for some seconds and put it on a towel afterwards to keep it warm as long as possible. If it cools down too fast you can but the tank into hot water until temperature is properly again.

    After about 10 minutes the board is ready. Take a closer look a nets which are close to each other to be sure that all copper is gone. I use my cell phone's camera which takes nice pictures much more detailed than I'm able to see without any optical support.
     
  6. Remove the resisting toner

    The last step is quite easy. Use Aceton to remove the toner. And after this the board is ready for further steps (for example drilling the holes).
Here some pictures I've taken:
My first try using a transparent film as transfer media
Another try: You can see light spots where dust was between the transparent film and the copper
Sometimes toner resists on the transparent film.
Special toner transfer media. Click to enlarge to see the patterns of the laser printers transport wheels.
A layout on copper using toner transfer media. Good and reproducible quality.
Etching the board - after 5 minutes.
Etching the board - after 8 minutes. Watch the bubbles!
Etching the board - after 10 minutes. It is nearly done - there is some copper left (click to enlarge).
Waiting for etching the last bits of copper.
The final result after remove the toner - pretty nice!
Another try using toner transfer paper: I wanted to know how good it is using bigger layouts.
The board you see is a breakout-board for TSSOP-14 ICs. I made a couple of them to get some practice in soldering SMD ICs. But this is another story...

Samstag, 8. März 2014

Control motors - Part 3: Digital PWM - Part 2

When I wrote "Control motors - Part 3: Digital PWM" I thought everything would work fine but time told me different (in German we say: theory is gray but practice is colored).

The first thing I built was the clock. To make development easier I used a slow clock and put a LED at the output to visualize the signal:
This was pretty easy.

The next step was to install the counter. One thing I had to change was to count down instead of up because presetting to 1 by using the cascading output did not work: the signal is high from the beginning of 0 until the end of 14, becomes low in the middle of 15 and high again at the end of 15 (which is equal to the beginning of 0). I had to invert the signal since the preset-pin is low in inactive state but this means that the rising from low to high used to preset a value is in the middle of 15 - not at the end! This shortcuts the last unit.
A possible solution could be preseting the value asynchronously (which means to tell the IC within a clock-cycle to use the presetted value at the beginning of the next clock-cycle) but unfortunately the ICs I bought did not support this feature. Another solution is to delay the signal for the half of a clock-cycle by inserting a capacitor. This didn't work either. Additionally this way seems unstable to me. I always prefer to use things in the way they ment and if an IC does not support a feature I have to find another which does.
At the end I had to count from 15 to 0 and preset the value to 15 once 0 (= minimum-pin active) is reached:
The LEDs from right to left: Bit 4, bit 3, bit 2, bit 1 and the clock-signal. The IC counts from 15 to 1 and then starts at 15 again.

In the last step I added the comparator IC. I simulated the input-values (the percent of the PWMs duty-cycle) which will be given by Raspberry Pi's GPIO-pins by using four microswitches - each for one bit. Let me show you the possible input and output-values:
Signal sent by GPIO-pins Condition Output-signal
0-0-0-0 = 0 > or = to counter always 0 (= 0%)
0-0-0-1 = 1 > or = to counter 1 if counter is 1 otherwise 0 (= 6.67%)
....
1-1-1-0 = 14 > or = to counter 1 except counter is 15 (= 93.33%)
1-1-1-1 = 15 > or = to counter always 1 (= 100%)
As you can see this is a clean PWM-signal from 0% to 100%. And here is the video showing the signal:
In the middle of the breadboard is a blue LED showing the PWM-signal. At the beginning there is no light because no microswitches are pressed which means input 0-0-0-0 (= decimal 0 = 0%). Then I pressed the switch for bit 1 which means 0-0-0-1 (= decimal 1 = 6,67%). Next I pressed the switch for bit 2 which means 0-0-1-0 (= decimal 2 = 13,33%). Afterwards I pressed the switch for bit 3 which means 0-1-0-0 (= decimal 4 = 26,67%). And finally I pressed the switch for bit 4 which means 1-0-0-0 (= decimal 8 = 53,33%). To complete the row I pressed all switches additionally which means 1-1-1-1 (= decimal 15 = 100%). In every state the output-LED shows the proper duty-cycle.

At the end I replaced the big capacitor of the clock by a smaller one and the LED switched from blinking in the rhythm of the PWM to lighten more or less brighter - as expected. But suddenly the 0% stage was not "no light" instead it was slightly lightening even hard to see. Where is this current coming from?
This short video shows the breadboard in the dark. The clock-LED turns on and off so fast that it seems to be turned on continuously. The blue-LED which shows the PWM-signal is slightly lightened although the input-signal is 0-0-0-0 (no microswitches pressed).

Before I tell you, I have to admit that I had an error in reasoning: For the motors I want to use I have to apply a 2kHz PWM-signal. So I bought a capacitor for the clock-IC (NE555) to produce this frequency. But in fact if the desired PWM-frequency is 2kHz and we divide each cycle into 16 steps the clock for driving the counter-IC has to run at 2*16 = 32kHz.
Finally I could figure out that what I saw was the result of something called glitch. It occurs if the rising from low to high is to slow and the IC is to slow or clock signals delay due to long distances. So rising from 2kHz to 32kHz made glitches visible. As suggested by Stefan (see "Control motors - Part 3: Digital PWM") I replaced all ICs by pendants of the 74LS series. This didn't solve but cleared out the problem as these ICs switch faster:
Every time a cycle completed (counter has reached 0 and will be presetted to 15) the glitch is visible by a short blink of the output-visualizing LED.

But what is the solution? Generally it seems (as Google says) that a buffer should remove glitches. A buffer is a combination of two NOT-operations in serial. The resulting signal is the same as the original input-signal. Glitches should be removed because usually logical IC accept voltages below 1/3 as low and above of 2/3 as high. If the glitch is lower than 1.67V (5V * 1/3) than the buffer should clear out the correct signal. But this did not work for me. I assume that my glitch is near to 1/3. So I had to find another solution.
Finally I figured out that this also can be achieved by using a flip-flop. A flip-flop is the mechanism which enables a computer's memory to store binary stages (true or false => 1 or 0 => high or low). They work in this way: You have to provide an input-signal and tell the flip-flop to store that input. From now on, even if the input changes, the stored signal is provided at the output. There are several types of flip-flops but I used the D-type which depends on a clock signal. So the solution is to ignore values (also glitches) within clock-cycles and use the value at the beginning of a cycle (doing a snapshot by using the flip-flop) during the whole cycle. And indeed it works like a charm.
This was the first problem in electronics which I solved on my own. No Google - just thinking. I was very proud at this moment :-)
The ICs from the left to the right: flip-flop (SN74LS74AN), or (SN74LS32N), comparator (SN74LS85N), counter (SN74LS191N) and time (TLC555). In this version I replaced the microswitches by pull-up-resistors.

In the next weeks I will build a lead frame and a circuit diagram for this. I will update the post by adding these two things as soon as this is done.

Freitag, 14. Februar 2014

PIC - LVP

After reading the post-title you might ask yourself: What does PIC mean and what LVP? I can give you no answer - at least for "PIC". I can tell you what a PIC is: It is a microcontroller produced by Microchip (see https://www.microchip.com/pagehandler/en-us/products/picmicrocontrollers) and it seems that nobody knows what PIC stands for. But I have to admit that I did not search very long for an answer. Before I explain what LVP means I want to explain what a microcontroller is: It is a programmable chip (IC).
At some point in time you will see that building complex functionality by an electronic circuit is (nearly) impossible. Or from another point of view: Before I knew about microcontrollers and I saw a tiny circuit board which did complex things and was built only on top of couple of ICs I asked myself how this could work. Or "Do they build an IC for every possible problem?" - of course not. These boards are driven by microcontrollers.
A microcontroller is a tiny computer: A tiny EPROM to store the program, a little bit of RAM, sometimes an additional flash to store states and a tiny CPU (usually a RISC-unit) specialized to the skills of each type of microcontroller. I will use a PIC 12F1840 which knows 49 instructions - this is really a ReductionInstructionSetComputer!

At the moment I will not talk about how programs can be developed or by using which programming language. I will write about this in another post. I want to tell you how to transfer a given program into the microcontroller. This is usually done by a "programmer" which can be bought from $25 to $100 (and of corse: open end). When I realized that I also will have to use microcontrollers for at least one feature of my mowing robot I started to read up on what programmer I will need and which type of microcontroller I want to use. After a short while my head was full of questions: Which type should I use? Will I need more than one type? Even within the family of a specific type usually a programmer is not able to burn all chips of that family, so what programmer would be the best choice? At the end I did not know what to do or what to buy. Another aspect is that I didn't want to buy another gadget because my budget decreases rapidly. Suddendly I come up with the idea of that maybe it is possible to burn a microcontroller using the Raspberry Pi! And indeed: It is possible!

Asking Google I found a couple of projects. The complexity of the underlying circuits where quite different. The reason for this is, that usually microcontrollers are burned using "high voltage" which is about 9V-12V. Computers work at 5V so either there has to be an external power supply or there has to be a step-up module. Addtionally all signals has to be level-shifted (I will write a separate post about level shifting because we will need this, too) which also blows up the circuit.
But there where also easy circuits and then I found it: The easiest circuit!


If you take a closer look you will see that the chip is wired to the Raspberry Pi's 3.3V-pin. And here is LVP: Low Voltage Programming. There are microcontrollers which do not need high voltage for burning a program.

As it is so easy and the 12F1840 microcontroller is sufficient for my needs I went to the next electronic store, bought the chip (€ 1.41) and wired up the Raspberry Pi as shown in the drawing. I downloaded the package (https://github.com/danjperron/A2D_PIC_RPI) and tried to burn the program. Guess what: It worked!

pi@raspberrypi ~/burnLVP-master $ sudo ./burnLVPx.py A2D_PIC_RPI-master/RpiA2D.hex
File " A2D_PIC_RPI-master/RpiA2D.hex " loaded
Scan CPU
Check PIC12/16...
CpuId: 0x0
Cpu Id = 0x0
Revision = 0x5
Found PIC12F1840 from Cpu Family PIC12/16
Cpu Id: 0x1b80 revision: 5
Bulk Erase Program , Data. .... done.
Program blank check................................Passed!
Data Blank check........Passed!
Writing Program................................Done.
Program check ................................Passed!
Writing Data.Done.
Data check .Passed!
Writing Config......Done.
Config Check......Passed!
Program verification passed!


An image of my Raspberry Pi wired to PIC 12F1840 as shown in the drawing above


In an upcoming post I will tell you what can be done by using the RpiA2D program and the 12F1840. So stay tuned...

UPDATE:
You will need to install the Python-lib intelhex to run burnLVP:

wget "http://www.bialix.com/intelhex/intelhex-1.5.zip"
unzip intelhex-1.5.zip
cd intelhex-1.5.zip
sudo python setup.py install
sudo ./burnLVPx.py

Mittwoch, 29. Januar 2014

Run Raspberry Pi by battery

In my last post we discussed what things have to be considered on running the Raspberry Pi and the motors by battery. I decided to use a 12V battery. Due to the facts that the Raspberry Pi needs a 5V power source and the battery's voltage will vary according to its state of charging we need something to get along to these problems: A step-down constant voltage module. There are two types of step-down modules: Linear regulators and voltage converters.

Linear regulators are used to step-down voltage only for a small amount because it is done by transforming the difference of voltage into heat. This is not very efficient but can be done if there is enough electric current available (for example if your circuit uses a power supply unit instead of a battery). In fact our Raspberry Pi uses a linear regulator to step-down 5V to 3.3V. I found several blogs showing that this regulator can be removed to run the Raspberry Pi at 3.3V because 5V is only needed for USB and the 5V-supply of the connector-unit (doing so breaks your warranty!). Gpio-pins for example also run at 3.3V. But I won't modify my Raspberry Pi since I will also need 5V for other purposes.

Voltage converters work similar to PWM-signals: The source current is switched on and off at a very high frequency to get an average voltage as output. But this frequency would be transported by the current to components running at the output voltage (for example ICs) and disturb their functionality. So the output has to be flattened. This is done by adding inductors and capacitors. This method is very efficient especially if the voltage has to be stepped down extensively. The only limitation is that the difference between the input and the output voltage has to be at least 1.5V to get this mechanism to work.

As I want to preserve as much energy from the battery as possible and the difference between the battery's voltage (12V) and the circuit's voltage (5V) is more than 1.5V I decided to use a voltage regulator. Now to get a constant voltage even if the input varies you need an IC which will handle this properly. I have chosen the LM 2576 T-5.0 (see
http://www.reichelt.at/ICs-LM-2000-LM-25576/LM-2576-T5-0/3//index.html?ACTION=3&GROUPID=5466&ARTICLE=10425&SHOW=1&START=0&OFFSET=500&) which is able to handle currencies up to 3A. Usually the Raspberry Pi needs up to 700mA on average and the circuits needed to control the mowing robot won't need more then 300mA so I also could use the LM 2575 T5.0 which is limited to 1A. But I read that the Raspberry Pi might produce peeks up to 2A so I think using the 2576-IC is the conservative result.

The 2576-IC's data sheet (see http://www.reichelt.at/index.html?&ACTION=7&LA=3&OPEN=0&INDEX=0&FILENAME=A200%252FLM2576T5%252C0%2523NSC.pdf) shows how to build the electronic circuit which is pretty simple:
As suggested in the data sheet a second input capacitor at 100nF (ceramic capacitor - not shown in the circuit above) and a second inductor-capacitor-pair (output ripple filter) should give a good power supply for the Raspberry Pi:

With this post done I shared all information I figured out until now. In the next weeks I will build and test all the circuits shown in my last posts (including this post). I will update each post by appending results, photos and conclusions of applying this knowledge in practice or add separate posts if necessary. So stay tuned…

UPDATE:
I built the circuit. Here I measure the input-voltage (11.27V) provided by a wall-cube-regulator (should be 12V but this is a good test as batteries also supply different voltages depending on their state of charging):
And of course the result (including descriptions of the elements on the breadboard):

Samstag, 25. Januar 2014

Control motors - Part 4: H-Bridge

As I mentioned in my last post I have to wait for the delivery of the parts I ordered. In the meantime I will continue blogging. So what we've got until now is: We have chosen a proper type of motor, we have discovered how to control the speed of the motors and we figured out how this can be done by Raspberry Pi's gpio-pins. If we want to plug these things together we have to buy a test-motor to be sure that everything works as expected.

One of the characteristic features of a motor is the voltage needed to run in full speed. But unfortunately most motors have different voltage values as Raspberry Pis 5V output. Additionally the 5V port gives insufficient power to run one motor let alone two of them (see http://elinux.org/RPi_Low-level_peripherals). So it seems that we need different power sources for the Raspberry Pi and the motors. This would allow us to choose the motor (nearly) independently to the current the Raspberry Pi needs to run. There is one thing to be considered if doing so: the motors voltage value should not be too close to 5V.

Finally the mowing robot's power source will be a battery. For the sake of simplification I decided to connect the motors to the accumulator directly. The controlling Raspberry Pi has to be connected to a battery, too. However, accus usually have voltages of multiples of 1.2V. So you will get 4.8V with using four cells or 6V with using five cells. But the range of voltage for Raspberry Pi's power supply is between 4.75 and 5.25V. Additionally fully charged batteries have more voltage than nearly discharged ones.
So we need a step-down constant voltage module if the batteries voltage is higher than 5V or a step-up if it is lower. Those modules work best if there is enough scope between the source voltage and the destination voltage (I read it has to be at least 1.5V).

So as I have to use a battery different to 5V (at least 6.5V) I can connect the constant voltage module to the motors' accu, too. The only condition is that the nearly discharged accus voltage is at least at 6.5V. I have chosen 12V. The main reason for this is that the motors I want to use (http://m.conrad.at/ce/de/product/233132/) are only available at 6V or 12V and 6V is too less.
I also want to mention that it would be possible to use 8.4V accus and two step-down converters: one for the Raspberry Pi's 5V and one for the two motors. In this situation I could also use the 6V motors. But I read that motors of higher voltages need less electrical current because the electrical resistance is lower as higher the voltage is (this is also the reason for using high voltages in high-tension lines).
Another challenge is to enable the motor to run in both directions (forward and backward). This can be done by exchanging plus and minus. But if the robot has to go backward we cannot unplug the motor and plug it in the other way again. This has to be done by an electronic circuit. Additionally the circuit has to manage the connection of Raspberry Pi's 5V and the motors 12V. Both features are covered by an h-bridge (see http://en.m.wikipedia.org/wiki/H_bridge).

If you do some research you will find out that there are many other things which has to be considered in conjunction with h-bridges: Motors produce inductive currents on stopping which have to be discharged, the PWM-signal has to be applied to the motors power supply, not running at full speed will produce heat which has to be dissipated and also monitored. There are many things which can go wrong so I decided to look for an IC which is able to handle all these things for me. What I found is this http://www.shop.robotikhardware.de/shop/catalog/product_info.php?products_id=89 (I'm sorry this site is written in German). Only a few parts have to be assembled and once done the motor can be controlled by four pins at 5V-level:
  • IN A at high and IN B at low: The motor goes forward
  • IN A at low and IN B at high: The motor goes backward
  • IN A and IN B at high or low: The motor breaks if rotating
  • PWM: Input-pin for the PWM-signal
  • GND: The 5V-level ground
...and four pins at 12V-level:
  • POWER and POWER-GND: The place where the battery has to be connected.
  • MOTOR and MOTOR-GND: The place where the motor has to be connected.
Pretty easy, isn't it?


Once I have received the parts (including the IC used as the h-bridge) I will do the assembling and report about it in an upcoming post.

Donnerstag, 23. Januar 2014

Control motors - Part 3: Digital PWM

During the last days I did some research because I'm about to order several parts needed for upcoming posts. But now this is done and I'm ready to share further findings.

After exploring PWM-signals I thought about how to control pulse width by using Raspberry Pis gpio-pins. At the very first moment it seemed easy: Take four pins and set any binary value controlled by software (0-15 means 6.66% for each step which is sufficient for my needs). A circuit will manage the current flow through resistors which values match the binary bit each gpio-pin represents. But studying the PWM-circuit shown in my previous post discloses that electric current has to flow in both directions through my resistors and using the potentiometer the value of the resistor on the way back is complementary to the value of the one the way there. This is what builds the typical PWM-signal. Building a circuit based on gpio-input behaving in this way seems to be pretty complicated. There has to be a better way to do this.
After a little bit of searching the web I found this page: http://www.spaennare.se/pwmdig.html#chap6. The guy who shared this information (his name is Stefan) is pretty smart and found an indeed elegant way to solve this problem:
The cycle of a PWM-signal can be divided into units which number is defined by the resolution at which we want to control the output current. I my case this is 15 because I want to use four gpio-pins (2^4=16 stages and 15 units between them). Stefan has chosen 100 stages so his circuit is a little bit more complicated as mine will be (but more precise). Each unit can now be at current (called high) or off current (called low). So for example if we want 13.33% output we have to keep the first two units of one PWM-cycle at high and the remaining 13 units at low.
So how could Stefan achieve this pattern? He COUNTED from zero to 100 and COMPARED each value to the requested (and inverted) level of current. If the second value is greater then it is high otherwise low. And this is periodically repeated. If the counter reaches 100 it starts at 0 again. COUNTING is done by the IC 4510 and COMPARISON by the IC 4585. The output of IC 4585 is exactly the PWM-pattern we need.
But his approach has three downsides which I want to get rid of:
First disadvantage is that he uses decimal units (100) which is not a numeric system supported by computers well. Computers use binary numbers because they only know high and low (a bit). You can use 7 bits to represent numbers up to 127 or 6 bit for numbers up to 63. 7 bits wastes some space and 6 bits is to small. So using any potential of two for the number of steps will fit better to Raspberry Pis gpio-pins. So I will use the IC 4029 which is a binary counter instead of the IC 4510 which is a decimal counter.
The second downside is that his circuit needs an inverted input-signal for working properly. Inverting the signal is not hard to do (Stefan gives an example circuit) especially if it is provided by a computer: simply invert the number by software before setting the gpio-pins. But inverting is not necessary as you will see.
The third downside is that Stefan uses 100 stages from 0% (motor does not rotate) to 99% (motor rotates at almost maximum of speed). This missing step to get 100% of speed is not a problem at all. But if you use 4 bits (as I do) every missing step is 6.25% and a maximum speed of 93.75% is a problem!
So what have I done:
In my descriptions above I said that each step is 6.66%. This is because I use 16 stages which represent values from 0% to 100%. Common binary counters (as IC 4029) use four bits and allow cascading for counting bigger numbers than 15 (they start at 0). As we use four gpio-pins we do not need to cascade (as Stefan needed to count up to 100) what simplifies the circuit.
Next is that I do not invert the input signal. So I also have to choose another output pin (in fact two of them: = and >) of the IC 4585 to get a proper PWM-signal.
The last action is to preset the counter at 1. This reduces to number of units by 1 but raises the output of the input-signal "1111" to 100%.
So at the moment I'm waiting for receiving the parts I ordered (including ICs 4029 and 4585). As soon as they arrive I will test my theory and update this post by appending the circuit and pictures. So stay tuned!

UPDATE: I added another post ("Control motors - Part 3: Digital PWM - Part 2") to comment what happened during building the circuit.

Freitag, 17. Januar 2014

Control motors - Part 2: PWM

"Control motors - Part 1" showed that there are less downsides on step motors than on gear-backed motors. Nevertheless to move a mowing robot of a weight of about 10kg (sometimes uphill) we need a force of about 2-4 Nm per wheel (having a diameter of 12 cm). Only gear-backed step motors can put about this level of force and those are rare and expensive. If you put gears between a wheel and a motor the motor has to rotate at the rate of the gears faster to achieve the same rotating speed of the wheel. A step motor needs 200 impulses to turn its axis around. So using a gear-backed stepper with a gear-ratio of 1:50 results in doing 26315 steps per second to gain a speed of 1 meter per second. As I found in the web it seems that stepper motors are limited to 2000 sometimes 3000 steps per second. Even if there is a stepper out there which is twice as fast it would be 4-5 times to slow. Another fact is that step motors have less torque the faster they turn. So I think this is it: I need to use ordinary gear-backed motors.

So the first challenge at using non-step-motors (ordinary dc-motors) is to control their speed. If you look at the specification of a dc-motor you will find the voltage they need to run. This value is valid for the full speed. If you want half of the speed you have to halve the voltage you putting on. But how can we achieve this by using Raspberry Pi's gpio-pins? As I said in my preceding post we have to use PWM (pulse width modulation). But what is PWM and how does it work?

PWM means switching an electric current on and off very fast. Depending on the frequency of this switching the average voltage changes. This is very useful for digital controllers (as our Raspberry Pi is) which only know 0 and 1 (current off or on).
But PWM is a special way of switching current on and off: Within a period of a choosen frequency electric current is switched on and off proportionately. I found this image at http://www.societyofrobots.com which shows how it works very well:

These three examples use all the same frequency but different pulse width (electric current is switched on) and they produce different values of average voltage.

For my first actual project I decided to build PWM as a curcuit. In the electronic tutorials I looked at the NE555 IC was used to produce timed signals. I bought the parts for exercising those tutorials, so let's ask the world for instructions. I googled and found http://www.domnick-elektronik.de/elek555.htm (written in german) showing this tiny curcuit:

I built this curcuit on my breadboard and put a LED and a proper resistor on OUT (pin 3 of NE555). Guess what: The LED dimed by turning the potentiometer. For educational reasons I replaced C1 by 10μF and voilà: The LED blinks in the way of the PWM signal.

UPDATE:
Here is a video showing the result:

Donnerstag, 16. Januar 2014

Control motors - Part 1: What type of motor to use

The first thing I want to do is to control a motor. For a mowing robot we need several motors: Those which drive the wheels and the one which drives the cutting-rotor.
Maybe this is the right moment to think about steering: As other robots do, I want to use three wheels. Two wheels at the back which drive the robot and cannot turn. One wheel at the front which is not driven but can be turned around at random. Here is an image: http://www.ratgeberzentrale.de/uploads/tx_news/15260_23529_Bild3.jpg. It does not show the configuration of the mowing robot I have in my mind but it does shows how the three-wheels-concept is meant.
In this configuration steering to the left is done by braking the left wheel or accelerating the right wheel. Steering to the right vica versa. An advantage of this technique is the ability to turn around nearly in place. A disadvantage is the need to control two motors precisely which means to use more gpio-pins than if we would drive our robot like a common car (I will explain the reason for this in an upcoming post).
So how is a motor controlled by an electronic circuit?
One possibility is to use a stepper-motor. These motors allow to turn a predefined step (for example 1.8°) forward or backwards. Repeatingly doing steps results in a continuous rotation. Speed is controlled by the interval of those steps.
The advantages of those motors are that they are easy to control and the force needed to do a step (even if you are driving uphill) is regulated by them self. Another feature is that they are able to hold their position even if a force tries to change the position. A downside of this is that they consume energy even if they are not rotating. Another disadvantage is that step motors are not built to rotate fast. They are made to move sheets of paper in printers forward and backward or support balanced robots for example. So it might be that this kind of motor is not suitable for our application.
The second possibility is to use back-geared motors. They are quite simple: put voltage on it and the axis starts to rotate. This kind of motor is not able to rotate slowly so the gear slows it down and brings additional power due to its gear ratio. This sounds pretty good but there are downsides!
The first one is the way to control the speed of rotation. This is done by reducing the voltage. If you have a regulator this is done easily but how can this be achieved by Raspberry Pis gpio-pins? The solution for this is PWM (pulse width modulation). This technique is supported by Raspberry Pi nativly but only for one device not two (motors). So we have to build PWM on top on usual gpio-pins. The second downside is that driving uphill would slow down the speed  of the motor and voltage has to be increased to compensate the reduction of speed. To do so it is necessary to measure the current speed - also not so easy.
As you can see the decision is not easy to come.

How Raspberry Pi can control things

Our Raspberry Pi supports several ways of interacting with other systems. As we have to develop electronic circuits to interact with the environment we have to use the gpio connector (see image at http://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup/overview). Gpio means "general purpose input output". This is the easiest way to control other circuits (output) or to get feedback from them (input) into the software running on the Raspberry Pi.
What is easy to me? Easy means that output-pins of the connector can be switched on or off by software commands. Current flows or not, there is nothing in between. In the same way input pins can only measure whether current is off or on. This is quite useful when we develop circuits because we can easily simulate active and inactive pins by using a micro-switch. Pushing the switch lets the current flow and releasing it stops the flow. So we are able to build and test circuits without them being really connected to the Raspberry Pi. Connecting the Pi and replacing our manual control by proper software can be done later.
But what about values between on and off - for example the speed of a motor we want to control or the distance to a barrier we want to measure? Those values have to be translated by using more than one pin. This technique is called analog-digital-converting. If we want to control 4 levels of speed (of a motor) we have to use two pins: 0% (00), 33.3% (01), 66.6% (10), 100% (11). If this is not as accurate as needed we can add another pin: 0% (000), 14.3% (001), 28.6% (010), 43% (011), 57.3% (100), 71.6% (101), 86%, 100% (111). As you can see this procedure needs a lot of pins and the number of available pins is limited. So we have to see whether we need a work around or not (for example a port expander).

Mittwoch, 15. Januar 2014

The goal

In the past I always needed a reason for doing something. Also in this case. So what useful thing can be created using a tiny computer? Nothing, if it doesn't interact with its environment. Interaction by reading or writing information to other computers is well known so another challenge is needed: Internet of things.

As I understand it "Internet of things" is every device or gadget driven by internet technologies. In the huge number of possibilities I picked something I could use well: A mowing robot. Related to the Raspberry Pi this will conduct me to a lot of challenges like electronics in general, controlling motors to move the robot, sensing the robots environment, building a battery-backed system and finally writing the software needed to steer through the garden safely.

Maybe this goal is to high to reach, but it will be a lot of fun trying. So let me see, what can be achieved...