Näytetään tekstit, joissa on tunniste electronics. Näytä kaikki tekstit
Näytetään tekstit, joissa on tunniste electronics. Näytä kaikki tekstit

torstai 7. tammikuuta 2021

Touch sensing for Arduino

I made a small christmas project (from which I might make a post later) in which I ended up using an Arduino Pro Micro (or rather a clone, Open-Smart Pro Micro). I also wanted to have two capacitive touch sensors to set brightness of a light, but the Atmega32U4 in that Arduino board does not have touch sensing circuitry build in.

I know that it is possible to still implement a capacitive touch sensing using the built-in I/O ports so I tested some methods and write about my experience here. I ended up using a method I haven't seen around so I will also explain that and link to the code.

Concept 1

The first concept is what is built-in (or at least easily installed) as a library in the Arduino IDE. The library is called CapacitiveSensor and is rather easy to use. One sensor needs two I/O pins and a large value resistor, in addition to the sensor itself (which can be a piece of aluminium foil, for example).

The basic connection is according to figure below. The left side shows circuitry internal to the microcontroller.

 

The touch sensor is connected to one I/O pin (the "drive" pin) via a resistor, which is typically mega ohm or tens of mega ohms. The sensor is also directly connected to another I/O pin (the "sense" pin).

The operation is depicted in following figure.

The drive pin on the top is configured as an output. The sense pin on the bottom is configured as an input, without pull-up resistor. With this circuit, the touch is sensed based on the time constant of the resistor and the capacitor formed by the touch pad and possibly the finger. Touching the sensor increases the capacitance to the circuit ground, which increases the time constant and that can be sensed. The operation of the circuit is as follows:

  • The drive pin is set to output a high level (red line)
  • The sense pin (blue line) is read until the pin status changes to indicate a high level in input
  • Timer is started (or current time is read)
  • The drive pin is set to output a low level (green line)
  • The sense pin (blue line) is read until the pin status changes to indicate a low level in input
  • Timer is stopped (or current time is read)
  • The time between the input state transitions is calculated

It is recommended to read the time from high-to-low and from low-to-high and calculate the average to increase the robustness of the circuit.

By monitoring the time between state transitions a change can be detected which indicates that the sensor is touched or touch is removed. At least in theory. In my tests, the sensitivity was very poor and it was difficult to detect touch robustly. Especially if any other electrical equipment was close to the sensor. I tried to add some grounded (to the circuit ground) plates near the sensor but that did not help at all.

Concept 2

This concept is very similar to the previous one but might allow getting rid of the external resistor and would use only one I/O pin per sensor. This could be achieved using the internal pull-up resistor of the microcontroller, according to figure below.

 Again, the internal parts are on the left. Rpu is the internal pull-up resistor, value of which is 20...50 kOhm in the Atmega32u40 (so quite small). The basic idea of this circuit is the same as previous one, but implementation would be a bit different.

  • Configure the I/O pin as output and drive it low to discharge the touch pad capacitance (green line)
  • After a (short) delay, start the timer
  • Configure the pin as input with pull-up resistor (red line)
  • Read the state of the input pin until it indicates a high level in input (blue line)
  • Stop the timer and calculate the time it took to charge the touch pad capacitance

I did  not try this, and the pull-up resistor value might be too low to make this circuit feasible (i.e. the time to charge the capacitance would be too short to measure accurately). It would be quite easy to test though, maybe some day...

Concept 3

The third concept I tested was something I came up with from reading how the real touch sensor chips work. I have not seen this concept used with Arduino, but I might be wrong. It uses only one pin per sensor and only needs the sensor connected to the pad. I didn't test the concept with single sensor, but at least with two it works well and probably is easy to expand to more sensors. One downside is that the sensors must be connected to analog input pins.

The concept looks like this:

The left side shows again the microcontroller internals, and this time I drew also the analog input sample and hold capacitor Csh, which is 14 pF in Atmega32U4 and the sampling resistor which is 1...100 kOhm (and is not important). I also drew the parasitic capacitance between the two touch sensors, value of which changes when a finger is brought close to the sensors.

The operation can be explained as follows when reading the upper touch sensor:

  • The lower (other) sensor pin is configured as output and driven low (green line)
  • The measurement pin (upper) is configured as output and driven high (red line). This charges the touch pad capacitance to the supply voltage level
  • The measurement pin (upper) is changed to analog input and read. This triggers the sample-and-hold circuit which charges the capacitor (blue line) and then reads it value with the ADC (orange line)
  • A change in the analog value read indicates whether the touch pad is touched or not (higher value means touch)

The key here is that the sample-and-hold capacitor is 14 pF and the touch sensor capacitance is typically in the same range, maybe tens of pF. When the touch pad is first charged to the supply voltage level and then the analog circuitry is connected, the charge in the touch pad capacitance is divided between the touch pad and the sample-and-hold capacitor. This means that the voltage is also divided in the ratio of the capacitances. Touching the sensor increases the capacitance of the sensor, which means the voltage that is sampled will be higher.

Driving the other sensor low also increases the sensitivity, as there will be capacitance between the sensor measured and the other sensor, especially when the sensor is touched. I tried driving the other sensor high also, but driving it low gives the best result.

I uploaded the code for this sensor stuff to GitHub here. Feel free to try it out and comment if there is something to improve.

Here is also a picture of the method in operation. In the figure below the green line is the raw ADC value (sensor is read 20 times with 1 ms in between to get rid of 50 Hz mains hum, and the values are summed together), blue is low-pass filtered signal and red is the threshold value where the low-pass filtered value is compared.

There are 4 touch events where the signal goes significantly higher than the base line. There is roughly 20 % increase in the reading when the sensor is touched and when it is not, and there was a thick (several millimeters) insulator between my finger and the sensor.

Conclusion

I ended up using this ADC based capacitive divider method which might be something not used before with Arduino. I got very good results with it so I want to share it to others to test and improve upon. Maybe it helps people to create even better projects :)
 






lauantai 12. syyskuuta 2020

Salora Äänimerkkilaite for VR

Some years ago a neighbour of mine got his hands on a little box, which was marked "Salora Oy Äänimerkkilaite" and it had something to do with old VR trains (VR is a Finnish railway company). His relative has a thing for trains, so he thought that the device might make a good present for him, so he asked me to check what the device does, does it work and would it be possible to turn it into a present.

I'm writing this post because I thought this might be of interest for other train hobbyists also.


Background

The device was a little black box with text "Salora Oy", "Äänimerkkilaite" (sound signaling device) and a serial number. On one end there was several pins for connection to some kind of backplane. In a figure below, the device is shown together with my wires connected to some pins.

 


Luckily, the device came with a paper which had the schematic and layout, so there was no need to reverse-engineer the device! From the schematic, it was obvious what the device was and how to operate it. Only thing left was to verify whether it works or not.

 

The device is a sound generator, which creates a familiar "ping-pong-ping" sound which can be heard before announcements are made from the train's loudspeakers. The device only consists of three oscillators creating the tones, a trigger circuit, and an summing amplifier to produce the output signal. Everything is analog. There is also an internal 8.2 V regulator for power supply;  the device is intended to run from 15 V DC.

 

Operation

Here I'll explain the device's operation with annotated schematic. I did this in LTspice, and all the component values are not the same as in the real device to keep simulation time short. For example, some time constants are much shorter in this version.



First, the blocks circled with green are the oscillators. According to the paper schematic, the desired frequencies are, from top to bottom, 659 Hz, 523 Hz and 784 Hz. All the blocks have a trimmer resistor which can be used to fine-tune the frequency. Output of each block is a square wave with the desired frqeuency, going from 0 V to 8.2 V.

 

The device is triggered with trigger circuits marked with blue squares. Each channel has its own trigger with an RC delay on the left, then two inverters with a hysteresis resistor. Whenever the input goes high, the inverters create a clean rising edge, and input going low, the output also goes low. The trigger circuits are chained so that top one triggers first, and then starts charging the RC delay of the middle one, which finally starts to charge the RC circuit of the last one. The time constant of the delay is roughly one second.

 

The first, topmost, trigger circuit is triggered from one of the input pins through a 1 k resistor, yellow block. Whenever the signal should not sound, the pin is pulled down. When the pin floats, the trigger circuits operate and a sound is played.

 

The red blocks are high-pass circuits which create a short, exponentially decaying pulse. The pulse goes high when the device is triggered, and the decays to 0 V with about 1 second time constant.

 

The exponentially decaying pulse is used as the volume signal for each oscillator. Exponentially decaying waveform gives a bell like sound. The "mixing" is done with two diodes, marked with purple squares.

 When the output pulse from the high-pass filter, red squre, is positive, one of the diodes in the mixed (purple square) conducts. When the oscillator (green) is low, it is the diode to the left which conducts and no current flows from the mixer to the output. When the oscillator is high, the left side diode is blocking and current flows through the right side diode. The current amplitude depends on the exponentially decaying voltage of the high pass filter (red). I think this is quite an elegant solution.


The rest of the circuit is just filtering of the square wave outputs of the oscillators and mixing them together with an audio amplifier. Finally, the sum of each oscillator is passed through a DC-blocking capacitor and a potentiometer for setting the volume. This is shown with brown square.

 

Checking the operation

Now that I knew what the device does, it was time to verify whether it works or not. I attached a laboratory power supply to the power pins, a pushbutton between the trigger and ground and using oscilloscope I probed the oscillators, trigger signals etc., and finally, as everything looked ok, also the output. I saved the output waveform as csv, and on my computer I converted it to wav, and it sounds like this.

 

As the device seemed to work, I added some circuitry around it. First, because my pushbutton was a normally open, I needed to do a simple transistor inverter for it. This way, when the button is pressed, the trigger is let float and otherwise the trigger input is pulled down by the transistor.

Second, I added a mono audio amplifier circuit and a speaker. The amplifier was very simple, needing only minimal amount of components around it. I think the amplifier was LM386, high voltage version, in DIP-8 case.

Third, I added a DC jack so that I could use an old laptop power brick, outputing 15 V to power the circuit. Here is a picture of the final working device.



Finished device

My neighbor did a nice case for the device, so that he could give it away as a gift. Here is a video showing the case and operation of the device.




 

tiistai 2. huhtikuuta 2019

Modifying an FM radio

TL;DR I tried to modify an FM radio that only goes up to 108 MHz to receive weather satellites at 138 MHz. That failed, but I learned thing or two on the way.

Introduction

I read about receiving weather satellites with PC sound card and a modified radio many years ago, and since that time I've always wanted to do that. Today it is rather simple to do with very inexpensive software defined radios (SDRs), but using pre-built components is only half the fun.

The weather satellites I talk about transmit at 137 to 138 MHz using FM modulation. The easy way to receive that is to use a radio scanner and connect it to PC using the sound card line input and the software in previous link decodes to images. A powerful antenna may be needed, but that is just details ;)

Radio scanners tend to be quite expensive compared to normal FM radio receivers. Where I live, the FM radio band goes from about 88 MHz to 108 MHz, so quite close to what I needed. I decided to try and modify an existing radio to reach the required band.


Selecting a radio

I looked at the most inexpensive receivers and especially one that looked to have an analog tuning mechanism. I thought that one with analog tuning would have some LC tuning circuit, which I could modify to an upper frequency to extend the band upwards. I assumed that ones with digital tuning (e.g. buttons to tune up and down) might be hard to modify.

I ended up buying a Prego FM/AM radio which had analog potentiometer style tuning and volume. Below is a picture of what the radio looks like.


 

Opening it up

I opened the radio and looked at the PCB. There was no obvious tuning circuit (coils and capacitors) or even crystal oscillators visible on the board.

On the top side there was few IC circuits and on the bottom side just one. The bottom side one was a power amplifier driving the speaker, so what I wanted was one on the top side.

On the top side there was some regulators and interestingly a EEPROM of type L24C02B. This turned out to be important but I didn't know it yet.

The radio chip was under some plastic, protecting it from the mains transformer. The type of the radio receiver is KTmicro KT0922. Luckily I found datasheet for it.