Using a Thumb Slide Joystick

Here are some notes on using the Sparkfun Thumb Slide Joystick on an Ardunio. This is a small two way X-Y variable resistor based controller that uses two analogue pins on the Arduino and works with both the 5 volt logic of the Uno as well as the 3.3 volts of the Teensy.

Thumb Sliding Joystick
Thumb Sliding Joystick

I found that this joystick works well for giving the eight positions found at the stick limits crosswise and diagonally but it doesn’t seem sensitive enough for reporting accurate positions.

Connecting

On the underside of the joystick there are four solder pads, looking from above with the screw holes pointing towards you, the connections are:

  1. X-Axis
  2. +5 volts
  3. Y-Axis
  4. GND
Connections, looking from above and circuit diagram

Should you take one to bits, you will find two sliders with small metal contacts and a circuit board with carbon tracks acting as a variable resistor. The sliders move in a parallel configuration, and the joystick control has a spring to return it to centre when released.

Inside the Joystick showing the contacts
Inside the Joystick showing the contacts
6N2A5558
Carbon tracks on the circuit board, solder pads top left

These can be tricky to put back together afterwards.

Underside of the joystick, with some wires soldered to the connections
Underside of the joystick, with some wires soldered to the connections

The pads on the base are spaced at 2mm but with some bending it is possible to attach standard 2.54mm (0.1inch) header pins, solder the inside two first before attaching the outers.

Thumb Joystick test setup with a Teensy
Thumb Joystick test setup with a Teensy,

When mounting in a case or panel you will need to drill a 18mm hole for the controller and the thumb pad is raised about 3mm above the surface of the body. I would secure the controller in place with a couple of blobs of hot glue  rather than use the small screw holes.

Programming

For my setup I am using the Teensy’s 3.3 volt output to power the joystick and the X-Axis connected to Analog pin 0, and Y-Axis to Analog pin 1,

This test program shows the position of the joystick through the serial port, it attempts to calibrate itself and set the values for the chosen direction, if this is not always successful when returning to the centre position try experimenting with the tolerance and maxRange values.

Links

Hacking a Vacuum Fluorescent Display

I’ve had this Vacuum Fluorescent Display – VFD recovered from a broken DVD player in the bits box for quite a while. I shall turn it into a clock and have it display the date at the press of a button, it will also set its time using the internet and the Network Time Protocol over a WiFi connection, I’ll be using a Teensy 3.1 Ardunio compatible micro-controller as this is compact and with the addition of a crystal and button battery has a clock built in.

VFD Digital Clock

This project is a complete replacement of the logic and driver that came in the DVD player, I have also used general electronic components as these are what I have available: 74HC595 8-Bit Shift Registers, ULN2803A Darlington Arrays and PNP transistors, rather than dedicated VFD driver chips, such as the MAX6920AWP+, Philips NE594N or the TI SN75518N available from eBay. If I were undertaking this again I would use a dedicated driver.

How A Vacuum Fluorescent Display Works

The VFD is made up of three layers, starting at the back these are:

  • The Anodes – these are elements of your display that illuminate
  • The Grid – to control a block of Anode elements
  • The Cathode – also referred to as Filaments or the Heater
A VFD showing the white Anode elements with a row of three illuminated, next are the honeycomb lattice grids and the five Cathode filament wires on top.

“The filament is heated, which causes it to release electrons, a process called thermionic emission. Since electrons are negatively charged, if there is a nearby piece of metal with a electrical charge more positive then the electrons from the cathode, the electrons will be attracted to it, allowing a current to flow.

The grid is positioned between the Anode and Cathode. If the grid is driven more negative than the cathode, it repels the electron cloud, which prevents any current from flowing. Since the grid is not heated, it does not emit any electrons itself.

A VFD is basically triode, except the anode is coated with phosphor. Therefore, when the the anode is more positive then the cathode, the free electrons in the cathode’s electron cloud flow towards the anode, and in the process strike the phosphor, exciting it.” Connor Wolf on stackexchange.com

The display brightness is set by the difference in voltage between the Cathode and Anode. The Cathode filament should be set to a low voltage, no more than 4 volts as it is made from thin tungsten wires which can melt very easily. The Anode uses a higher voltage around 12 to 15 volts.

Multiplexing is used to display the output. Each grid controls a fixed number of elements, the element connections are common to each grid. For a clock you will need to loop between the grids selecting the correct elements to display before moving onto the next, the persistence of vision effect maintains an illusion of a complete display.

Further information

Connecting and testing your Vacuum Fluorescent Display

You will need to find the connections that your VFD uses as well as the best voltages to supply. Unless you are really lucky its unlikely you will find a datasheet for your display, so a little reverse engineering is required.

A selection of test hooks are very useful at this stage. You will also need two power supplies, one low voltage for the Cathode filament, I use 3.3 volts supplied from the Teensy/Arduino. For the gates and Anodes a variable DC supply you can adjust from 10 to 20 volts preferably with the current limited to 1mA, in some cases displays from older equipment will need a higher voltage, 50V and above. The ground on both your power supplies will need be connected together.

The flat VFD’s you see in consumer electronics, such as the DVD player, all have a similar layout. The two pairs of outer pins connect to the Cathode and the pins between these divide into two blocks one side will be for the grids and the other for the Anode elements.

First, establish that the pins you suspect are for the filament are indeed so, with your multimeter in continuity (beep) mode you should see a connection when you probe across these pins. Connect your 3.3v supply to these pins, it doesn’t matter which way round.

6N2A4212
VFD Element being illuminated, with 3.3V across the Cathode filaments (red and black connections). with the first grid on the white connection, and third from last element connected to the green.

Next, set your variable power supply to its lower setting and attach one probe to a grid pin, and the other to an Anode element, on my display I started with a pin furthest left and another furthest right, see if anything lights up, at a low voltage this will be rather dim. If an element has lit up has then increase the voltage until the brightness is what you would expect. If nothing lights then first check you have connected a grid and an element not two grids or two elements by choosing different pins. Or if you are reasonably sure then carefully increase the voltage.

When you are driving the display as a clock, the multiplexing will make each element appear dimmer I increased the display voltage to 15 volts to fix that.

VFD Pin mappings for my display
VFD Pin mappings for my display

Now you can map out the grids and elements, on mine, there are eleven grids the first and last grids are for special characters and the rest contain digits. With a Grid connected, go through each element and make notes, each element will have the same connection on each grid, so all the digit elements and special characters/icons have the same pins.

VFD pin numbers for the digits
VFD pin number mapping for the digits

Driving the Display

While the Cathode filaments are permanently on at 3.3V the 12V grid and Anodes require some kind of high voltage level shifter to have the 5V logic output from the Ardunio (or 3.3V logic on the Teensy) switch the 12V required at the display. The method I have used for switching is a NPN, PNP complimentary (Sziklai pair) output.

Transistor Control
Transistor Control

The NPN transistor is the switch controlled by the Ardunio this in turn switches the PNP transistor, the NPN transistor is being used to isolate the Ardunio from the high voltage required for the display. As the current being drawn is very low we only need small signal transistors NPN: BC549 and for the PNP: BC556.

For the PNP transistor to switch off the Base voltage needs to be close to that of the Emitter, as the Ardunio’s 5V logic is nowhere near the 12V used a NPN stage is added so when the Base of the NPN is on this pulls the Base of the PNP low and allows current to flow to the display. There are three resistors, R1 100K limits the current to the base of the NPN transistor to protect the transistor and Adrduino, R2 10K is biasing the output, working as the bottom half of a voltage divider, and R3 10K is both the top half of the voltage divider and pulls the PNP Base high keeping it off when not required.

Transistors as switches, further reading:

To reduce the component count and size of the project I have replaced the NPN transistors and the resistors R1 and R2 with ULN2803A Darlington transistor array, however the PNP transistor remains as the display requires a sinking output to provide a grounded connection to the load through the cathode.

Two of the four drivers, with, from the top, the Registers, Darlington Arrays, and PNP transistors
Two of the four drivers, the other two are underneath. with the Registers, Darlington Arrays, and PNP transistors.

Connecting to the Arduino

The next stage is to connect the 32 pin display to the Arduino. Obviously the micro-controller does not have enough I/O for this, so instead I have used four easy to use 74HC595 8-Bit Shift Registers with only a data, clock and latch to set up, they can be used to extend the number of output pins on the Arduino.

74hc595_serial_schem
74HC595 Serial Connections (click to enlarge)

I have used the ShiftOutX library for my clock, but it helps to look at the Ardunio’s ShiftOut tutorial to see how they work.

Power

To power this Digital Clock I am using a mains to 12V DC brick from some old electronics equipment, I then use a Buck power supply to provide a 5 volt supply for the Teensy and electronics, and for the display a Boost supply to bump the 12V to 15V, search for XL6009 Module on ebay, check that it has the XL6009 rather than the older LM2577 and set the output voltage before connecting the display.

Digital Clock Power Supplies, 5 volts, small board on the right, 12V to 15V from the larger board on the left

The Driver Circuit

Here is a quarter of the driver circuitry, for the 32 pin display I made four of these. You can see that there is an extra transistor, as the Darlington Array has seven inputs/outputs and also note that on the 74HC595 8-Bit Shift Register the Ardunio data pin connects to DS (pin 14)  and the next register in series connects to to Q7S (pin 9) of the previous register.

VFD Driver circuit (click to enlarge)

Software

This demonstration uses the ShiftOutX library. It loops through each grid and each element within that grid, it does not use multiplexing.

This second demonstration counts from 0 to 9999, with the digits right aligned on the display, it uses multiplexing and the display will appear dimmer than before:

Sources and References

Upgrading the Python Oracle Client

This is a follow up to one of my previous postings: Python and the Oracle Client. The main databases here are being upgraded to Oracle 12 and I’ve taken the opportunity to update the client used by my Python scripts, also its good practice to install new clients when old versions go out of support.

Current Setup

The system I am upgrading here has the following configuration, but this should work with any RPM based distribution, such as CentOS and SUSE :

  • Red Hat Enterprise Linux Server release 6.6 (Santiago)
  • Python 2.6.6
  • python connector: cx_Oracle – 5.1.2
  • oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64
  • oracle-instantclient11.2-devel-11.2.0.4.0-1.x86_64

To find the versions of your currently installed software:
$ python
Python 2.6.6 (r266:84292, Nov 21 2013, 10:50:32)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
>>> print cx_Oracle.version
5.1.2

and the Oracle client:
$ rpm -qa | grep oracle
oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64
oracle-instantclient11.2-devel-11.2.0.4.0-1.x86_64

Preparing

If you have the old versions installed you will need to do some tidying up by removing the client and python connector, version 11 of the client despite being RPM packaged had some non-standard elements. Use rpm to delete the old version of instant client, remove devel first:
$ sudo su
# rpm -ev oracle-instantclient11.2-devel-11.2.0.4.0-1.x86_64
# rpm -ev oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64

you may also need to remove the library reference from a previous installation:
# rm /etc/ld.so.conf.d/oracle.conf
# ldconfig

to remove the Python oracle connector, there are two methods. Manually, by finding the previously installed package deleting the files and editing the package list:
# find / -name cx_Oracle.py -print
/usr/lib/python2.6/site-packages/cx_Oracle-5.1.2-py2.6-linux-x86_64.egg/cx_Oracle.py
# cd /usr/lib/python2.6/site-packages
# rm -rf cx_Oracle-5.1.2-py2.6-linux-x86_64.egg

now edit the easy-install.pth file
# nano /usr/lib/python2.6/site-packages/easy-install.pth
and remove the line:
./cx_Oracle-5.1.2-py2.6-linux-x86_64.egg

Or do it the easy way, if you have pip installed:
# sudo pip uninstall cx_Oracle
easy_install does not have an uninstall option.

Installing

Download and install version 12 of the Instant Client and SDK (devel), these can be gotten from: http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html For Linux choose the correct flavour for your installed operating system: x86 or x86-64 for 64bit operating systems, you will need to register on the site gain access the files.
# rpm -i oracle-instantclient12.1-basic-12.1.0.2.0-1.x86_64.rpm
# rpm -i oracle-instantclient12.1-devel-12.1.0.2.0-1.x86_64.rpm

Now to install the python connector:
# easy_install cx-Oracle
or, the recommended method:
# pip install cx-Oracle
Installation for the version 12 client is much more straight forward than that for version 11.

Testing

A quick test to ensure that the expected versions appear, and that you can connect to the database.
Python 2.6.6 (r266:84292, Nov 21 2013, 10:50:32)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
>>> cx_Oracle.version
'5.2'
>>> oraConn = "<USERNAME>/<PASSWORD>@<DATABASE HOST>:<DATABASE PORT>/<SERVICE>"
>>> ocDB = cx_Oracle.connect(oraConn)
>>> ocDB.version
'12.1.0.2.0'

Sources

Water Splash Photography

Two drops of water colliding, frozen in time using the power of high speed flash photography produce an infinite variety of shapes. While this can be done with a pipette, a camera, a single flash gun, practice and good hand/eye co-ordination. I have an Arduino Uno and I am going to use it. IMG_8887What is happening in this picture? Two carefully timed water droplets have been released from above and are plummeting towards a bowl of water. The first drop has hit the water and is rebounding, just as the up-spout reaches its zenith, the second drop collides with the top resulting in a mushroom shaped splat, with the event captured in the camera with a frame of 1/10,000th of a second. In this post I’ll be sharing my experiences in creating these water drop images, I’ll be looking at the photography equipment, electronics, and technique.
up meets down

Photography Equipment

Camera: This can be any DSLR or advanced compact, it must have Bulb mode, and be triggerable by an electronic wired connection, some have an IR remote but I found this to be difficult to setup. Set the ISO to be around 200.

Lens: I use a 100mm Macro, with focus set to manual and image stabilisation off. The aperture is set high, at least f22 to give a suitable depth of field and improve image sharpness.

Tripod: A good solid one with easy to adjust ball head.

Flash: I use up to five flash guns for my photos, two for back light, one to give an under-light through the glass bowl, another for front light and finally one handheld. Rechargeable batteries for the flashes are recommended, I use 2400mAh NiMh Duracells.

The flash guns need to be in manual mode at their lowest power setting, this is to give the shortest duration of flash for the sharpest results. As you increase the flashes power the duration of the light emitted gets longer, causing burred images. On my Canon flash I set it to 1/128 second and on the Nissin Di622 set the EV to -1.5.

6N2A5694  6N2A5698

For connecting the Arduino to the flashes I use a 2.4GHz wireless remote trigger, with four receivers and a modified hotshoe mount attached to the transmitter Look for the Yongnuo RF-602 Remote Flash trigger on ebay, (not to be confused with the remote shutter release).  Most modern TTL flash guns appear to be missing the wired remote trigger connection that you can just plug into.

The flashes also have a built in slave trigger, where it sees that one flash has gone off so it set itself off too. On the Canon flashes this appears to only work in ETTL mode and can’t be used for this, but the Nissins work well.

Hardware

The frame is bits of wood held together with glue and stands about 75cm high this is to allow the water to accelerate and produce decent sized splashes. At the base is an extra large seed tray, the type without holes, to contain any spillages. This normally has a glass bowl full of water acting as the drip splash event zone. Halfway up the fame is mounted the laser and detector and at the top a reservoir of water and solenoid valve.

The reservoir is a one litre plastic storage tub from Poundland with a hole drilled in the base and a short length of 8mm PVC tubing hot glued into place. The tubing can be difficult to glue as its rather flexible, pushing down a short section of solid tube made from the outer of a disposable biro fixes that. This pipe is connected to the solenoid, observing the correct direction of flow marked on the valve.

The reservoir has a Mariotte Syphon fitted to the lid, this is to provide a constant and stable water pressure to the valve, the pipe from the lid ends about 2cm short of the reservoir base.
resevoir

Electronics

The Arduino and control electronics are all set to produce this photo taking sequence:Trigger Prototype

  1. press ‘play’ button on remote control
  2. lights out – dark room
  3. open shutter on camera
  4. solenoid releases two drops of water
  5. drips pass through laser detector – timer started
  6. drops arrive and do their thing
  7. flash guns triggered by timer – picture taken
  8. shutter closed on camera
  9. lights on

The electronic circuit can be broken down into these five blocks; lights, laser, IR receiver, solenoid control, flash control, camera control, each diagram shows the label name for the pin used rather than a pin number. The diagrams can be enlarged by clicking on them.

IR Receiver: The IR receiver allows use of an old TV remote control. My original design was to have a rotary dial and a small OLED display, but this simplified everything considerably. If you don’t have a spare remote one can be gotten from Poundland. I have the Arduino send any text output to a laptop on the USB port.

ir_rx

Laser: Warning: keep away from eyes, permanent damage can occur with exposure to any laser. The laser is used with a photo-transistor to detect drips of water as they plummet to their splash event. I used a small 3 Volt 5mW red laser with a built-in lens, I have added a resistor and diode in series to prevent over voltage as they’re a bit delicate. Although a modified laser pointer will do just as well. The TEPT4400 phototransistor is a type rated for visible light and has higher sensitivity to change than a photoresistor.

laser control
Laser Control

Lights: Warning: Mains Electricity Can Kill, this is to be avoided. If you are uncertain about this part then don’t do it. I rapidly found that working in darkness between shots just made life difficult, and finding the light switch became a hassle. To fix that I got a pre-made 5v Relay circuit and wired this up to a table lamp to provide some illumination. Using a standard wall socket and backbox connect live through the normally open side of the relay, and the neutral and earth to the socket.

 6N2A5700 6N2A5705

Remember to keep the electricity away from fingers (and any other body parts) and water.

Solenoid Control: I use a 12v solenoid, (search for “12v solenoid valve water arduino” on ebay, a couple of sellers have suitable models with connectors included). I use a mosfet transistor to switch the power, this has been detailed in one of my previous blog postings.

solenoid_control
Solenoid Control

Flash and Camera Control: The electronics for the camera and flash are closely related. Both use the ILD74 optocoupler to electrically isolate the camera and flash equipment from the Arduino. Although the camera focus connection is not used here I have included it as it may be useful later on.

camera control
camera and flash

The Canon camera has two different types of wired connection on the shutter release depending on the model of camera, a standard three pin 2.5mm jack or a N-3 connector (search for “canon N3 connecting cable” on ebay). A list of connectors for other makes of cameras can be found here.

canon remote
Remote shutter connections for Canon

Sound: Although not used here, this setup works well with a piezo microphone for use with popping water balloons and the like, use a buzzer that is enclosed in a plastic housing with a hole on top and buzzes when DC power is applied. Connect the output to an analogue pin on the Arduino, your software can use a very similar method to that for the laser.

sound
Sound Detect
water balloon pop
Balloon pop with sound detector

Setup and Use

Have plenty of dish cloths or towels to hand, this can get a bit moist. Keep an eye on your camera equipment making sure it doesn’t get wet.

For setting up a shot I use a steel ruler with a magnet stuck to it I set the water dripping to make sure it lands where I want on the magnet, the camera is then focused on the magnet, take away the ruler and you have your properly focused splash event.

IMG_8502
Splash Hat on Magnet

Add colour with food dies, adding these to the reservoir seems to work best and keeping the water in the splashdown area clean. Guar Gum thickens the water and makes larger drops and bigger splashes, you only need to add a small amount, about a teaspoon per litre and you’ll need to sieve out any lumps before use. Fluorescein is quite entertaining when used with a UV lamp, adding a green glow to your splashes. Adding diluted water based paints to the reservoir can add a lot of colour, but has a tendency to block the solenoid.

Sparkly backdrops can be gotten from the craft section in stationers. An A4 sized (21cm x 30cm) sheet is normally enough. Try bouncing the flash off the backdrop.

It is all about experimentation, expect to take lots of photos, many of which will be poor. Make notes of timings when you get good results, when you get a good shot, very small changes in timings can produce fairly dramatic effects.

Software

Here is an Arduino sketch, press Play on the remote to start a two drip sequence. adjust the amount of time in milliseconds between the laser detect and flash – flashWait with Volume +/-: +10,-10, Channel +/-: +5,-5, Fast Fwd/Rev: +2,-2, and the time betweenDrips with 4 (+1) and 7 (-1).

Links and Sources

My water drop photos on flickr:

Two Drops of Water

Multiple buttons on one pin with an Arduino

Here is a circuit that allows you to use five buttons; up, down, left, right and select, on one analogue pin with the Arduino. It is a adaptation of one used on this LCD shield manufactured by DF Robot. I have used a 10K pull-up resistor and added a 100nF capacitor to help with debounce. The downside is that this can only detect one button press at a time, the button with the lowest resistor value is returned, and the output varies when different voltages are used to power it requiring an update to your software.

In this post I am looking to explore the effect of the existing resistor values, and how to choose your own resistors. This kind of circuit is called a multiple voltage divider or voltage ladder where each rung of the ladder, the switches in the diagram above, produces a different output voltage. Pressing one of the buttons on our circuit changes the output to the analogue port from 5V to a lower one determined by the number of resistors (R1 to R4) in series before the button, as you go down the ladder the output voltage to the pin increases. Here is a short sketch I used to test the circuit and show the voltage reading for each button:

The results from this are shown in the chart below, for the power I used the regulated 5V and 3.3V outputs on the Ardunio Uno, if you run this set-up yourself you may notice an anomalous reading when you release the button, more about this later. Note that a button press drops the power down to the recorded value, the pin is normally high (5V/3.3V) through the 10K resistor.

Resistance1 at 5v at 3v3
Btn Value Meter reading2 volt reading2 volt
S1 none 0 0V 0 0V
S2 330Ω 334Ω 31 0.15V 19 0.10V
S3 950Ω 964Ω 88 0.43V 57 0.29V
S4 1950Ω 1948Ω 166 0.81V 108 0.53V
S5 5250Ω 5270Ω 352 1.72V 230 1.14V
1: Total value of the resistors in series at the button. 2: this is an average, the actual reading can vary.

There are two columns for the resistance, the value marked on the resistors and the multimeter reading. It is best to use 1% tolerance resistors for this circuit to reduce the chances of the readings drifting close to the next. A good spread of output voltages ensures accurate switching.

All this works very well, but what to do if you wish to add more buttons, or have different output voltages? Looking at the circuit, each button can be seen as a voltage divider, such as shown in the diagram on the left. Where R1 is the 10K pull-up resistor and R2 is the sum of the resistors for the button being pressed.

With this simplified model we can calculate any missing value, but for now there are a couple of things to do; calculate the output voltage for the given input voltage and resistance and more usefully for us, work out a value for R2 to give us the desired output voltage.

Calculating the Output Voltage

Using Switch 2 in our existing circuit for example, with 5 volt power:

    \[ Vout = Vin \times \frac{R_2}{R_1 + R_2} \hspace{15 mm}with\ numbers:\hspace{4 mm} 5 \times \frac{330}{10,000 + 330} = 0.16V \]

And again with Switch 4. This has three resistors in series so the value for R2 in the voltage divider is: 330R + 620R + 1K = 1950 ohm:

    \[5 \times \frac{1,950}{10,000 + 1,950} = 0.82V \]

its rather satisfying when your calculations closely agree with the real world readings.

Setting the output Voltage

So, lets try working out some resistors based on the following specification: powered at 5V with five buttons, each 0.20V apart; S1: 0V, S2: 0.20V, S3: 0.40V etc. We know the input voltage, output voltage, and resistor R1, but not R2, here is the formula for finding the resistance of R2:

    \[ R_2 = Vout \times \frac{R_1}{Vin - Vout} \]

    \[ S2: 0.20 \times \frac{10,000}{5 - 0.20} = 417\X\Omega \hspace{5 mm} S3: 0.40 \times \frac{10,000}{5 - 0.40} = 870\X\Omega \hspace{5 mm} S4: 0.60 \times \frac{10,000}{5 - 0.60} = 1364\X\Omega \hspace{5 mm} S5: 0.80 \times \frac{10,000}{5 - 0.80} = 1905\X\Omega\]

These values show the resistance required for each switch and to find the resistor the previous resistance value needs to be deducted. As these values probably won’t be in the E24 Standard Value range and won’t be available in the shops I have the nearest available value in the Standard Resistor column:

Btn Resistance Resistor Standard
Resistor
S1 none
S2 417Ω 417Ω R1: 390Ω
S3 870Ω 453Ω R2: 430Ω
S4 1364Ω 494Ω R3: 560Ω1
S5 1905Ω 541Ω R4: 560Ω
1: actually the nearest is 510Ω, but 560Ω was the closest I had

Obviously, we will need to get this onto breadboard for testing.

Using the same sketch as before I re-ran the tests with the new resistor values, the results are shown below; I have also included an Expected column to show the calculated voltage using the method shown earlier.

at 5v at 3v3
btn Resistor Resistance expected reading volt expected reading volt
S1 none 0 0V 0 0V
S2 R1 390Ω 390Ω 0.19V 39 0.19V 0.12V 25 0.12V
S3 R2 430Ω 820Ω 0.38V 76 0.37V 0.25V 50 0.25V
S4 R3 560Ω 1380Ω 0.61V 122 0.60V 0.40V 81 0.40V
S5 R4 560Ω 1940Ω 0.81V 165 0.81V 0.53V 108 0.53V

That, to me, looks close enough to the 0.20V separation at 5V specified to provide some stable hardware. Here is a short sketch to demonstrate the use of analogue buttons, it returns a number corresponding to the button pressed. When you release a button its resistance changes momentarily to give the Arduino a false reading, to fix this I have added de-bounce functionality to this program:

With this substitution on R3, changing the desired 510 ohm resistor for a 560 ohm, It may be interesting to see what kind of tolerance range we may be able to use. So, for the 0.60V output the optimal resistor would be 494 ohm but we only have Standard Resistor values available and to find the resistance value for our calculation to find the output voltage we need to add 820 ohm (R1 + R2) to R3:

Resistor Resistance Volts
430Ω 1250Ω 0.5556V
470Ω 1290Ω 0.5713V
510Ω 1330Ω 0.5869V
560Ω 1380Ω 0.6063V
620Ω 14400Ω 0.6294V

Clearly, using four decimal places shows is that a 510 ohm resistor is rounded up to 0.60V, while the 560 ohm rounds up to 0.61V, so for our purposes both are suitable. The other resistors will be outside the specification.

Finding the Power Consumption

We also need to look at how much power our circuit is consuming, this is for two reasons: to ensure that our resistors are properly rated and that the power supply can supply enough power. The Ardunio Uno provides a regulated supply of 20mA at 5V and 50mA at 3.3V. The calculation is done with Ohms law:

    \[ I = \frac{V}{R} \]

So, in the original circuit the shortest path to ground is through Switch 1 and the 10K resistor:

    \[\frac{5}{10,000} = 0.0005A \times 1000 = 0.5mA \]

a very low current, this is the maximum the circuit can use. Optionally, to work out the power use for another button add up all the resistors before the switch, for example in the original circuit Switch 5 comes to: 15250 ohm

    \[\frac{5}{15,250} = 0.00034A \times 1000 = 0.34mA \]

Voltage Divider Calculations

Just to round things off, here are four formulae for finding any resistor and voltage in the divider circuit:

    \[find\ resistor\ value: R_1 = \frac{Vin \times R_2}{Vout}-R_2 \hspace{20 mm} R_2 = Vout \times \frac{R_1}{Vin - Vout}\]

    \[find\ the\ voltage: Vin = \frac{Vout \times (R_1 + R_2)}{R_2} \hspace{20 mm} Vout = Vin \times \frac{R_2}{R_1 + R_2}\]

Links and Sources

Solenoids on the Arduino with MOSFET power

I am needing to control a solenoid from the Arduino to do some high speed water splash photography. The Solenoid works at 12v and draws up to 2.5w when open, obviously a direct connection to the Arduino is out of the question. Looking in my parts bucket I found an FQP30N06L MOSFET and recalled that I’d used these to control a small motor.

The FQP30N06L MOSFET in a TO-220 package,
the heatsink tab is connected to the Drain

The FQP30N06L is a N-Channel MOSFET and can switch up to 60V DC at 32A, this is more than chunky enough for the solenoid. For use with the Arduino it needs to have ‘logic-level’ switching of 5V, looking at the datasheet for the Static Drain-Source On-Resistance – RDS(on) we can see a test condition where VGS=5V (or 4.5V) exists then we know the MOSFET is suitable.

Looking for logic-level volts in the On Characteristics (source: FQP30N06L datasheet)

The Circuit:

  • D1: 1N4002 Diode – provides surge suppression from the solenoid, this protects the MOSFET from inductive voltage ‘kickback’.
  • L1: the Solenoid or motor (a solenoid is just a linear motor)
  • Q1: FQP30N06L MOSFET being used as a switch
  • R1: 220R Resistor – current limiting, see below.
  • R2: 100K Resistor – this keeps the Gate closed when the Arduino output is off/LOW

The resistor R1 is there to protect the Arduino. On a MOSFET when a voltage is first applied to the Gate it can appear as a short to ground, limiting the current will prevent a surge that may cause damage. The Arduino supplies 5V at 40mA on a digital I/O pin.

A MOSFET switches when there is a charge at the Gate, the higher the charge the wider the gate opens and more current can flow through the Source to the Drain. Looking on the datasheet for our MOSFET the Gate Threshold Voltage VGS(th) shows a minimum of 1V and a maximum of 2.5V, this means that below 1V the gate is closed, at 1V the gate is ajar, as the voltage increases the gate opens wider until you reach 2.5V where it is fully open, power can go above this but the gate will not open any further. Going above 20V on the gate VGS will break the MOSFET.

So we need a resistor low enough to fully open the gates, but high enough to prevent the Arduino spluttering. So I chose to limit the current to 20mA and calculated the resistor with Ohms Law:
R = V / I
5V / 20mA = 250R (I used a 220R as that’s the nearest I had, this limits the current to 23mA)

Ground on the Ardunio is shared with the 12V power supply for the solenoid.

Programming

Connect the circuit to your Arduino, the MOFSET’s gate to a digital output and ground. You may want to avoid digital pins 0 and 1 as these are on the serial port and can cause the solenoid to rattle unexpectedly.

Here is the Solenoid equivalent of blink, with the solenoid circuit connected to digital pin 3 it is switched on and off once a second:

Comedy effect can be had by reducing the timings to milliseconds, although keeping that up for too long will probably knacker the solenoid.

Solenoid Control

References and Links:

Rotary Encoders in Colour on the i2c Bus

This is a follow-up to my previous writings on the subject of rotary encoders: Rotary Encoders on the i2c Bus. This time I am using the Sparkfun Rotary Encoder – Illuminated (RGB) (Part: COM-10982), this has the same rotary goodness as the SparkFun 12-step rotary encoder but with the addition of three LED’s to provide a whole host of colours on the rotating shaft.

Test Setup

Again I will be using the MCP23017 port expander to add 16 digital I/O ports to the Arduino via the i2c bus,  the rotary encoder part operates in the same manner as before and we can use the internal pull-up resistors to reduce the number of components. The LED’s operate with a common anode and the push button also operates on 5v rather than the usual switching to ground.

For my test setup I have connected the rotary encoder to GPA0 and GPA1, the push button to GPA2 and the Red, Green and Blue LED’s to GPA5, GPA4 and GPA3. Note the 10K pull-down resistor on the push button.

My program on the Arduino changes the colours as you rotate the shaft, you will see seven colours, to see more you would need to use PWM to control the LED’s brightness. With the common anode on the LED’s the logic for switching them is inverted, so HIGH = off, LOW = on. You will need the Adafruit MCP23017 Arduino Library.

Links