Digital potentiometer X9C103S and its use in a program with reading interrupt signals from a photoelectric sensor. Software debouncing of the signal loaded in the interrupt.

The aim of today's article will be to point out the possibility of using a potentiometer and a photoelectric speed sensor together in one project. As a result, the potentiometer will serve as an output for speed estimation for the higher system (the higher system will not be implemented or described in this article). In our case we will use 8 points attached to the wheel of the locomotive.

The transition of these points will be recorded by the RobotDYN photoelectric sensor. In a specific time interval, the number of transitions is counted and sent as the number of steps to a digital potentiometer, which is set to a certain resistance, which is linearly graduated.

Technical information about the vehicle (electric locomotive class 362 "ESO") - Railway 600 Vracov in Czech Republic
• vmax = 20 km / h / 3.6 = 5.55 m / s, (maximum locomotive speed)
• d = 0,45 m (wheel diameter on the locomotive bogie),
• circumference = pi * d = pi * 0.45 m ≐ 1.413 m, -> distance the wheel travels per revolution
• n = vmax / circumference,
• n = 3.93 times the wheel rotates in 1 second at a maximum speed of 20 km / h
• Ie. 3.93 * 8 pulses represents 31 pulses per second at a maximum speed of 20 km / h
There will be a pause of approx. 32.25 ms between the individual pulses at maximum locomotive speed. This interval can be used to distinguish two consecutive signals without taking into account the oscillation that may occur at the digital input.
Hardware for project:
• Arduino Uno / Nano (ATmega328P)
• X9C103S - digital potentiometer
• RobotDYN photoelectric sensor
Digital Potentiometer X9C103S
Potentiometer is a variable resistor. It is most often in a mechanical version with a runner, but digital potentiometers, which can be set electrically, also occupy a separate category. The setting is possible in the range defined by the catalog sheet. Digital potentiometers are designed with a certain increment (step), by which it is possible to increase its value as a minimum.

As standard, it has up to 255 steps with various ranges from 1kohm to XY kohm. The X9C103S digital potentiometer used for this implementation has a resistance setting range of 0 to 10 kohm, and can be readjusted in 100 steps that are identically graduated by 100 ohms. This potentiometer has a linear characteristic. The current load of the potentiometer at the output is max. 4.4 mA, therefore it is not suitable for power systems, but only as a signaling component.

In the current implementation, the potentiometer is set to the number of pulses * 100 ohms per unit time (eg 1 second). The output of the potentiometer is connected directly to the computer of the higher system, which visualizes the value on the display of the locomotive station. 3 data wires (CS, INC, U / D) + power supply are used to connect the X9C103S digital potentiometer to the Arduino.

RobotDYN photoelectric sensor
Photoelectric sensor (RobotDYN) is based on an infrared transmitter and receiver. By default, the receiver is able to detect a signal from the transmitter that transmits continuously. This receiver output is directly connected to the DOUT (Digital Out) output of the module. Thus, in the case of a point transition between the transmitter and the receiver, a logical 0-LOW appears at the DOUT output. The sensor also has an analog output, which we will not use in today's article, as it is not suitable for this application.

Arduino is able to read this digital signal in an interrupt, dealing with the falling edge of the signal from HIGH to LOW. This phenomenon with a falling edge is also called FALLING. LOW interrupt does not respond to the interrupt type RISING (rising edge). An undesirable phenomenon that occurs is signal ripple, which can be caused by various phenomena, such as signal instability, poor filtering, slow point transition in front of the sensor, external interference and others ...

As a result, it can cause one real transition to be read by several times due to oscillations. There are various ways, especially for optimizing such a signal in hardware, for example by using a capacitor that can eliminate the oscillation, often extending its length by overcoming the voltage level. The oscillation is very short, lasting a maximum of a few milliseconds. The solution is also a software debounce, where the interrupt FALLING responds to the transition from HIGH to LOW level, but we also use time verification whether there are at least x milliseconds between the signals. Since we know from the previous calculation that there will be 32 milliseconds between pulses at the maximum speed of the locomotive, we can choose a quiet 15 to 30 ms debounce.

This will ensure that we only load the next signal and not ripple the existing signal with jitter. A similar debouncing was used, for example, in the project with the Hydreon RG-11 rain gauge. All variables that are used in an interrupt must be of the volatile type to handle their interrupt, regardless of where the program is currently executing. The interrupt handles variables of the volatile type even if the program is stopped on the delay() function. In our case we have volatile variables for the number of pulses (int) and time (unsigned long) read from the millis() function.

Interrupt (in the case of an Arduino Uno board) can only be used for terminals with hardware interrupt support, i. D2, D3. The system uses the millis() counter, which disconnects the interrupt once every 1000 ms. The Arduino then writes the value of the read signals as the number of steps per digital potentiometer, resets the counter, and then resets the interrupt. The time of 1000 ms for evaluation is only exemplary and is fully configurable in the system via the UART monitor, where it is possible to add and subtract 100 ms by entering the characters + or - (for testing purposes and finding the optimal reflection time). As it is possible to write values 0 to 100 on the digital potentiometer X9C103S, we must not exceed 100 steps.

It is therefore optimal to record pulses of up to 3 seconds (31 pulses per second at a maximum speed of 20km / h) to guarantee the number of steps below 100, or we can average / divide them by the number of passes, thus obtaining the number of wheel revolutions as such, which can be in the solution of a higher system that works with the output of a digital potentiometer.

Resistance Conversion to Visualizer
Recalculation itself is already performed in the higher C # system by a computer application that determines the current vehicle speed and can also visualize this speed on the locomotive monitor. Since the update takes place by default every 1000 ms, i.e. the refresh rate is 1 Hz, the change in speed on the monitor - analog visualizer (analog hand voltmeter style) is not jump. The computer is connected directly to the output of the X9C103S potentiometer. Potentiometer X9C103S has a certain deviation in the set resistance value in the range of 10 to 20%, in reality there is about 12%, which must be taken into account.

Program implementation is available at Gist Github: https://gist.github.com/martinius96/d1befbb807ff76562e634fa7c9cf00c5