Wednesday, December 11, 2013

Controlling Oscilloscope with Linux Computer

I know it's not something you would need on an everyday basis (rather lifetime basis), but wouldn't it be cool to be able to control your instruments using a script? Or let's say you want to enjoy the comfort of your desktop chair, while remotely controlling and getting data from your instrument in the workshop? With this mindset I started looking for ways to control my oscilloscope with my small eee pc with linux on it (CrunchBang 11, to be exact).


For a windows user you would install whatever software the oscilloscope manufacturer provides, which would place a "visa32.dll" file in your windows directory. This file would make it possible to communicate to the oscilloscope with the VISA API, and pyVISA could be used to control the scope with the programming language Python. For linux, we cannot rely on the luxury of the manufacturer supplying drivers (one can only have faith for the future). At first I tried to find a visa32.dll equivalent, but that didn't lead anywhere. It turns out that a full-worthy solution DOES exist, thanks to someone called Alex Forencich. He has made a pure Python driver based on USBTMC, and a IVI like wrapper on top of that (also in Python).

Before starting to install his work, PyUSB is needed:
sudo apt-get install python-usb

Unfortunately, for some reason, PyUSB is not installed that easily (see link). You also have to run
sudo apt-get install python-pip
sudo pip install --upgrade pyusb 

Now we're ready to install Python USBTMC (look for "Download ZIP"). Navigate to the folder where you downloaded the zip file and run:
unzip python-usbtmc-master.zip
cd python-usbtmc-master
sudo python setup.py install

Python USBTMC is now installed. Clean up by removing folder and zip file:
cd ..
sudo rm -r python-usbtmc-master python-usbtmc-master.zip 

Try it out:
ulf@eee:~$ python
Python 2.7.3 (default, Jan  2 2013, 16:53:07)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import usbtmc
>>>

All seems fine, but let's not get our hopes up too soon. At this point we need to figure out what the vendor and product ID of the instrument is:
ulf@eee:~$ lsusb
...
Bus 001 Device 004: ID 0957:179a Agilent Technologies, Inc. 

The first ID is the vendor (0957), and the one after the semicolon, the product ID (179a). Note that you need the "0x" in front of the number to indicate that it's hexadecimal (hate to admit it, but this caused me quite some hair loss). Alternatively you can convert it to decimal and use that directly.
ulf@eee:~$ python
Python 2.7.3 (default, Jan  2 2013, 16:53:07) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import usbtmc
>>> instr = usbtmc.Instrument(0x0957, 0x179a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/usbtmc/usbtmc.py", line 165, in __init__
    if self.device.is_kernel_driver_active(0)
  File "/usr/local/lib/python2.7/dist-packages/usb/core.py", line 719, in is_kernel_driver_active
    self._ctx.managed_open()
  File "/usr/local/lib/python2.7/dist-packages/usb/core.py", line 70, in managed_open
    self.handle = self.backend.open_device(self.dev)
  File "/usr/local/lib/python2.7/dist-packages/usb/backend/libusb1.py", line 733, in open_device
    return _DeviceHandle(dev)
  File "/usr/local/lib/python2.7/dist-packages/usb/backend/libusb1.py", line 618, in __init__
    _check(_lib.libusb_open(self.devid, byref(self.handle)))
  File "/usr/local/lib/python2.7/dist-packages/usb/backend/libusb1.py", line 571, in _check
    raise USBError(_str_error[ret], ret, _libusb_errno[ret])
usb.core.USBError: [Errno 13] Access denied (insufficient permissions)
>>> 

Darn... What to do now? Luckily Alex knew about this and had some pointers in his readme file. As indicated by the error message, we're dealing with a permission problem. The instrument has to be added to the usbtmc group. Create a file called usbtmc.rules in the correct folder:
sudo nano /etc/udev/rules.d/usbtmc.rules

Add the following lines to it:
# USBTMC instruments

# Agilent DSO-X 2004A
SUBSYSTEMS=="usb", ACTION=="add", ATTRS{idVendor}=="0957", ATTRS{idProduct}=="179a", GROUP="usbtmc", MODE="0660" 

Create group:
sudo groupadd usbtmc

Add your user to the group (change "ulf" to your own user name):
sudo usermod -a -G usbtmc ulf

Restart the computer and try again:
ulf@eee:~$ python
Python 2.7.3 (default, Jan  2 2013, 16:53:07) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import usbtmc
>>> instr =  usbtmc.Instrument(0x0957, 0x179a)
>>> print(instr.ask("*IDN?"))
AGILENT TECHNOLOGIES,DSO-X 2004A,MY********,02.35.2013061800
>>> 

It works! In fact, you can stop here and be satisfied if you want. At this point you can fully control your instrument. For example, if I want to stop my scope and then invert channel 1, I will write:
instr.write(":STOP")
instr.write(":CHAN1:INV ON")

It's, however, not a very convenient way to control your instrument. The IVI foundation has standardized how to use the C, COM and .NET programming languages to simplify the control (as described here), but does not say anything about Python. Therefore Alex has written his own Python interpretation of the standard, called "Python IVI". There are drivers for quite a few instruments, and luckily my oscilloscope is one of them.

To install, as before, download the zip file, extract, install and clean up:
unzip python-ivi-master.zip
cd python-ivi-master
sudo python setup.py install
cd ..
sudo rm -r python-ivi-master python-ivi-master.zip

Now, try it out (replace ******** with the serial number of your instrument):
ulf@eee:~$ python
Python 2.7.3 (default, Jan  2 2013, 16:53:07)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import ivi
>>> mso = ivi.agilent.agilentDSOX2004A("USB0::0x0957::0x179a::MY********::INSTR")

If there are no error messages, you're in control! Now, if you want to for example retrieve all data points from on measurement on channel 1, write:
waveform = mso.channels[0].measurement.fetch_waveform()

Or if you simply want to enable channel 4:
mso.channels['channel4'].enabled = True

Coolness achieved.

Sunday, October 20, 2013

AC Dimmer Repair


Ever been annoyed by flickering lights of one of your light bulbs at home? It's likely to be caused by your dimmer. In my case, besides constantly checking if I'm suffering from epilepsy (so far I'm safe), it was also making an annoying sound.

First step in trying to repair anything is to open it up and check for obvious problems, like burst capacitor or visibly over-heated components.


Nothing obviously wrong there. At this stage I decided to go all the way and find out exactly how such a dimmer works. If I succeeded, I thought, I should be able to find the broken component.

For this, I found a very good link with a lot of information. In addition, I decided to simulate the circuit with LTspice (You can find the LTspice files here).


For details on how such circuits works you can check the link, but in general, part of every half-period of the 50 Hz sine wave from the socket is cut off. In other words (actually, pixels), like this:

TRIAC dimmer

In that way the amount of power that is sent to the lamp can be regulated, which is seen as changing brightness.

All of this is achieved by having a capacitor being charged up (C2) at different speeds. When a specific voltage has been reached, the circuit is suddenly opened (with U2) and current flows through the light bulb. The speed of which the capacitor is charged, is varied by increasing or decreasing the resistance of the current that flows to it (R4).

The DIAC seen in the schematic (U1) is not strictly necessary; the TRIAC (U2) does the actual work of releasing the current to the light bulb. The DIAC is there to make the point in time when conducting starts more predictable. To test if any of the two was broken, I simply bridged over the DIAC by soldering a small wire. Except noticing that the transition from zero to full brightness was less smooth (could be expected when no DIAC is present), the flickering still existed. Therefore my conclusion was that the TRIAC was to blame.

I de-soldered the TRIAC and went to the local electronic shop to buy a new one. Viola! It worked. No flickering, no sound, no epilepsy.

Monday, July 29, 2013

Box for Pandaboard

You might think making a box is simple...

I'm using an ARM board called the Pandaboard as a media station which works fine, but it doesn't look that good in a living room (despite the fact that I partly share Benders excitement of naked electronics). I thought I would just make my own box, since at the time nothing was available to buy. I did not quite realize the amount of effort needed. However, after putting my sweat and blood into it (literally, as we shall see), in the end I can say that I'm pretty satisfied.

I wanted the following features for my box:
- As small as possible
- Have space for a hard drive
- Have all buttons, LEDs and connectors available
- Look like something you want to put in a living room

I decided to make the box out of wood, partly because it lets through the wireless interfaces and partly because it's easy to work with ('easier' that is). I must admit that the nice look of wood also made a difference.

I knew I needed to be very exact in measurements (sub mm) to get a decent result. Therefore I decided to make a complete model in Sketchup. At the same time I could play with different solutions and see if it looked ok.


Anybody who also has a Pandaboard and wants to do something similar, or simply want to use the model of the board to know exact positions of all the connectors, can find the drawing here.

The sides of the box are made out of solid wood, and the top of film faced plywood. I chose it because I liked the contrast of the dark brown and the beige wood. To make precise cuts I borrowed a Japanese saw from my father. I must say that it's a huge difference compared to conventional saws. Your eyes and steady hands are the limits rather than the tool.  For most of the holes I used a drill press (also owned by my father).


There are two LEDs on the Pandaboard, and with the software build I'm using, one is showing the processor activity in a heart-beat fashion, and the other, usage of the SD-card. Obviously I want to see them also after encapsulating the board, so I made two small holes in the front panel. To lead the light I needed some transparent glass or plastic. I looked around to see if that was something you can buy of the shelf, but found nothing. Instead I got a piece of thick plastic from one of my girlfriend's makeup jars and started making one myself.


The light should be directed in 90° so I made an angle of 45° in the plastic, and made it as shiny as possible with a fine file and some leather. 45° will be enough to give total internal reflection, assuming a refraction index of 1.5 of the plastic. To fit the pieces in the front panel holes, I filed them round at one end, and made them small enough to just barely fit in the holes (that way they will stay in place without glue). I also made sure to matte the visible surface with sand paper. My idea is that the light then will get diffused at the surface, and be equally visible from any angle.


I noticed that a lot of light leaked over from the LED close by, so I put some electric tape on the sides. Works quite well! (see the last picture of this blog post to get an idea).


I put the hard drive under the board, and to make the box as small as possible I needed to modify the usb cable to be angled. The first one I made didn't work because the cable was for usb 1.1 only (doh!).







Since I permanently use the hard drive and need one USB slot for a wireless keyboard, it felt like a good idea to add some slots. Fortunately the Pandaboard has two slots on the PCB which just needs a connector. Even more fortunately, the double slot connector I bought could be placed directly on the PCB! Might be of interest to anybody else who simply wants to add two USB slots. Make sure the pinning of the board (find the manual here) corresponds with the pinning of the USB connector. In my case, the pitch was slightly different for the holes and the pins, but not enough to be a problem. Also make sure to isolate the bottom of the connector! Otherwise you're likely to cause a shortcut somewhere. I used electrical tape and pressed the pins through.







Perhaps the most delicate part was to put all the wooden pieces together. Even though I made an effort to be precise when cutting them, there were small variations. In the end the best way was to fasten all four sides in a position that looked good (as seen in the picture below), and glue them on one by one.



I extended the buttons of the board in the simplest way I could come up with: two holes, two screws. To make it look better I polished away the slots on the heads of the screws, and made a thin indent in the wood. I wouldn't say it's impossible to do it with a normal drill, but it's a huge help to use a drill press.



After using the box for a while I noticed that it became pretty hot inside, despite the air holes on the lid and on the bottom. I had a small computer fan laying around which I thought would work. It was rated at 12 Volts, and the Pandaboard works with 5 Volts. That turned out to be only good, however. The air flow doesn't need to be immense, and by running it at the lower voltage it's much quieter.

 
I used two of the holes in expansion connector A for ground and 5 Volts.


As a final touch, I decided to give the box a name. I first experimented with using the brown top layer of the film faced plywood. This is where the blood comes in... I used a Stanley knife to try to peel off the top layer of a piece of the plywood. It slipped, and ended up in my hand.


I had apparently cut one of the tendons in my thumb, so the doctor had to find both ends of it and attach them together. Months of rehabilitation waited. You could at least have hoped that the idea was good, but when I some weeks later took courage to try again, it turned out the wood was too brittle for letters of the size I wanted. Instead I used a piece of fake leather from a sweater label. I used my friend the Stanley knife to cut the letters out, and a belt hole puncher to make the holes.







And here it is, in its rightful place:



Monday, May 13, 2013

Hard drive magnets

Do you have some broken hard drives laying around? Don't throw them away! You can extract and use the very powerful magnets inside. It's usually no problem getting them out, providing that you have the right tool (usually a small torx screwdriver). If you insist on some instructions, look at this link. Personally, though, I prefer to figure things out myself as I go along if I know there is no risk damaging something. It's your choice of course... I'm just saying... you probably learn more without....... I'll shut up now.

A warning is in place here. The magnets are very strong!!! If your finger get stuck in between two magnets, it will hurt. Even worse is a piece of your skin (Yes, it happened to me).

You can probably think of other ways of using them, but I decided to use them as hanging devices for tools.



In that way you don't have to buy hooks; just make the device yourself by screwing the magnets in place (provided that you kept the metal part that the magnet was mounted on). It's also much easier to attach the tool, since you only have to get close for it to snap into place.

Another, related, application is to hang jars from the magnets (metal caps required, obviously). My experience was that 3.5 inch hard drive magnets work perfectly for this purpose, while the 2.5 inch ones (left in the picture below) are a bit too weak.



You might wonder why two of the magnets are blue... That's because I intended to use them for the bicycle dynamo I already blogged about here. The blue part is shrink tube, which I closed with electrical tape to protect the magnets from getting rusty. There! Everyone's curiosity satisfied (expect perhaps those who wonder why Star Wars LEGO is hinted in the picture of a grown man's apartment...).


Thursday, February 21, 2013

Zero-friction bicycle dynamo


I wanted this non-friction dynamo for my bike that you can buy, but then I thought, why don't I just make it myself?

I've always preferred dynamos above battery powered lights. There are several advantages:
- No need to change battery
- No concern with turning it off when not cycling
- Most of the time you can turn it on with your foot while cycling
- The superior feeling of satisfaction, using your muscles to produce light

My experience is, however, that normal dynamos often fail. Sometimes they simply stop working without any hope of repairing them, which has happened to me a couple of times. Less serious, but still not acceptable, is that they rarely work when it's raining, or even less likely, when it's snowing. Sometimes the position of the dynamo changes slightly and has to be readjusted. Another annoying fact is of course that it becomes significantly heavier to cycle.

My idea was to instead use magnets, coils and LEDs to make a dynamo. In that way you should be able to make it more robust, and hopefully solve all the problems I mentioned above.

As always, I searched the Internet to find out what other people have done before (see links at the end of the post). I saw one solution where one magnet was put to motion by other magnets on the wheel, but I preferred another one, where the magnets on the wheel simply pass close by a coil with an iron core. No moving parts at all in other words, besides the wheel.

This is the circuit I used:



I got a hold of two good coils, so I decided to create one complete system each, as seen above, for front and back lights. Very simple, as you can see. I chose to add a rectifier bridge to make use of both the positive and negative voltage peaks. Each diode has a forward voltage, which will increase the amount of voltage needed before current is flowing, so I used schottky diodes (they have a lower forward voltage).

Most people seem to use voltage protection on their circuit (with a zener diode). I'm not sure it's needed. If you measure the voltage of the open circuit you will probably see a higher value than the LED data sheet recommends, but the coil will not act as a perfect voltage source. The voltage will drop to the forward voltage of the LED, and the current will be saturated to a maximum level. If you don't have an oscilloscope it will of course be difficult to know what this maximum level is. In my case, since I use three LEDs in parallel I figured the current wouldn't be too high for each diode to damage them. They have been working fine for over a year now.

To find a good coil, look for an old relay (I found a couple in a heap of electronic trash at my fathers place). Some work better than others, so you may want to temporarily connect it to the LED and pass a magnet over it to test it, before attaching everything to the bike.


I used shrink tube to protect the windings, as seen below.


I bought a rectifier bridge from the local electronics shop, and placed it straight on top of the coil. To know what wire is what, I used red shrink tube on the positive, and black on the negative. To keep it all in place, I again used shrink tube, covering both the rectifier bridge and the coil (not seen in the picture).


As a final step for the coil, I used even bigger shrink tube encapsulate the thing (I know, it's starting to sound like an obsession). I made holes for the iron core (which conveniently was threaded at the end) and the two wires, and sealed the ends of the shrink tube with electrical tape. To prevent water from getting in through the hole for wire, I covered it with silicon.

There was already a metal clip on my bike, probably for use with bicycle bags, so I used that to mount the coil. As with most other dynamos, I connected ground to the whole body of the bike (by simply sticking it in between the nut and the metal when mounting). Since I had two coils, I placed an extra metal piece and mounted the second coil at the end of it.


My first plan was to use magnets from an old hard drive to put on the spokes, but by coincidence I found two reelight magnets on a trashed bike. Besides the extra effort needed to mount hard drive magnets to the spokes, they should be equally good, or better. The hard drive magnets are without doubt stronger. If interested, see the links at the end of the post. I placed my magnets pretty close to the nave and for me that gave enough brightness. The further away from the nave they are, higher currents and therefore higher brightness you will get.



I got the LEDs from old battery lights. I had to scratch some PCB traces on the backside, and solder a bit together.



As a final step, I used the cover of an old 2.5 inch usb hard drive to protect the coils, and to mount a switch (the switch finally broke after turning it on with my feet too many times, so now the lights are always on)



And the result!


(It looks like it's blinking slower than it actually is because many blinks come between the frames of the video)
Possible improvements:
  • I'm using two magnets. I could add at least two more to increase the blinking frequency.
  • The magnets I use are not very strong. If I use some hard-drive magnets, the intensity should go up.
  • The intensity of the light can simply be increased by placing the coil and magnets further away from the nave. At the same time, you might be need to add some protection in your circuit.
  • If you don't want the light to blink (in some countries it's illegal), a capacitor can be placed in parallel with the LEDs. I've even read of examples where a capacitor big enough was used, to keep the light on when not biking for a minute or so. In that case it will, however, take a while to charge it up.

Solar panel for heating


My old man is planning to make a solar panel, after seeing this project:
http://24volt.eu/solfangare.php

Every winter he needs to heat up his workshop to avoid water pipes freezing, and occasionally make it warm enough to work in. Hopefully that can be avoided by placing a solar panel on the wall.

The plan is to replace a door which is anyway almost never use, with a glass and a dark background. Air holes above and below should provide enough natural air flow so that no fan is needed. Another difference, with respect to the link above, is that the air is not taken from outside. The idea is that this will increase the efficiency even more when it's cold outside. You can see an initial drawing below.


I'll keep you updated about the progress!

Mobile phone as oscilloscope

A friend from the Repair Café where I sometimes help out, showed me something really cool. He had adapted an old oscilloscope probe to be connected to the 3.5 mm mic/headphone jack on his android phone. Apparently there are several apps to be downloaded, that plots the microphone signal. Imagine having an oscilloscope in your pocket wherever you go!

Found a link about it:
http://www.nicomania.de/en/ipod/oszilloskop-tastkopf-fuer-ipod-iphone-und-android/

I want one...

Monday, January 28, 2013

Changing CMOS battery of eee PC 1005HA

The battery of my girlfriends netbook needed to be replaced (To avoid setting date and time every time you start the computer). I first wanted to buy one of those special ones including cable, but then I thought (mostly for fun) why don't I make one myself? I decided to buy a normal battery, and reuse the cable and connector of the old one. I used:

- Battery
- Shrink tube

That's it, believe it or not. The tricky part was to remove the metal clips attached to the old battery without destroying them. The second tricky thing was to place the clips is such a way that they would be pressed down by the shrink tube, both for the sake of electrical connection and so that they don't move around. When I think of it, tape might have helped... Result: