After I discovered the OpenStage project for cheap DIY microscopy stage automation, I decided to add a twist to it - control the stage positions manually with a rotary encoder, in addition to already-implemented serial port (USB).
I found a nice RGB illuminated rotary encoder from Sparkfun - it's shaft works as a button, and it is internally illuminated by built-in 3-color LEDs - a perfect device to switch speeds and manually control the stages.
Hooking it up to Arduino seemed easy, and there is a very nice Encoder library to do just that. But when I started to test it, I fell into a deep rabbit hole called 'debouncing'. In short, real-world switches are never perfect and the 'moment' of switching has many messy things happening between the two leads, creating noise in the logic of reading device (Arduino). So, the voltage readout from a real rotary encoder looks like this:
Note the high-frequency chirp in yellow line when it falls from high to low. The yellow and cyan lines are voltages at the two output leads of the encoder. The time interval on a grid is 5 ms, so things happen really fast here.
This chirp is called bouncing and needs, well, debouncing, either in hardware or in Arduino code. BTW, all push-buttons and mechanical switches also require debouncing for stable operation. Software debouncing is a simpler option, but this slows down the processor, which could be doing something more interesting then filtering out noise from encoders and buttons.
The hardware debouncing consists of a simple (RC) circuit, essentially a resistor and a capacitor, which works as a low-pass filter.
I changed the capacitor value to 0.1 uF, so that decay time constant RC = 10e+3 [Ohm] * 0.1e-6 [F] = 1e-3 sec, or 1 ms. With this circuit, the voltage readout from the rotary encoder looks much smoother:
The decay time of the debouncer circuit is 1 ms, as mentioned.
Now, 4 resistors and 2 capacitors add quite a mess to the breadboard, which becomes worse when you add 1 more capacitor + resistor to debounce the knob action of rotary encoder, and 3 more resistors to control the R,G,B LEDs. This whole mess just to filter the signals of a rotary encoder.
To my surprise and dismay, I could not buy a simple debouncing board in the Internet that would contain this circuit on a compact footprint. So I learned the Fritzing software and designed the missing PCB board myself. I then ordered it at AISLER, and 8 days later I had my PCB boards delivered, for only 4 eur a piece.
This filled me with deep satisfaction and accomplishment :-) If you are a DIY geek and have not printed your own PCB board yet - try it, this can make you happy and proud.
Making your own PCB is also a good way to document, pack and store a project for later use.
normal (30x50 mm),
small SMD variant (30x35 mm).
I found a nice RGB illuminated rotary encoder from Sparkfun - it's shaft works as a button, and it is internally illuminated by built-in 3-color LEDs - a perfect device to switch speeds and manually control the stages.
Hooking it up to Arduino seemed easy, and there is a very nice Encoder library to do just that. But when I started to test it, I fell into a deep rabbit hole called 'debouncing'. In short, real-world switches are never perfect and the 'moment' of switching has many messy things happening between the two leads, creating noise in the logic of reading device (Arduino). So, the voltage readout from a real rotary encoder looks like this:
Note the high-frequency chirp in yellow line when it falls from high to low. The yellow and cyan lines are voltages at the two output leads of the encoder. The time interval on a grid is 5 ms, so things happen really fast here.
This chirp is called bouncing and needs, well, debouncing, either in hardware or in Arduino code. BTW, all push-buttons and mechanical switches also require debouncing for stable operation. Software debouncing is a simpler option, but this slows down the processor, which could be doing something more interesting then filtering out noise from encoders and buttons.
The hardware debouncing consists of a simple (RC) circuit, essentially a resistor and a capacitor, which works as a low-pass filter.
I changed the capacitor value to 0.1 uF, so that decay time constant RC = 10e+3 [Ohm] * 0.1e-6 [F] = 1e-3 sec, or 1 ms. With this circuit, the voltage readout from the rotary encoder looks much smoother:
The decay time of the debouncer circuit is 1 ms, as mentioned.
Now, 4 resistors and 2 capacitors add quite a mess to the breadboard, which becomes worse when you add 1 more capacitor + resistor to debounce the knob action of rotary encoder, and 3 more resistors to control the R,G,B LEDs. This whole mess just to filter the signals of a rotary encoder.
To my surprise and dismay, I could not buy a simple debouncing board in the Internet that would contain this circuit on a compact footprint. So I learned the Fritzing software and designed the missing PCB board myself. I then ordered it at AISLER, and 8 days later I had my PCB boards delivered, for only 4 eur a piece.
Making your own PCB is also a good way to document, pack and store a project for later use.
Source files
The github page contains all PCB design files (in Fritzing) and Arduino code examples.Ordering
PCB can be ordered from Aisler in two sizes:normal (30x50 mm),
small SMD variant (30x35 mm).
In this blog about pcb board fabrication design involves creating a detailed layout of the circuit board, including the placement of components, routing of electrical connections, and preparation of design files for manufacturing.
ReplyDelete