Display ESP32 data on Android phone with BLE

Now let's move on to the inductor reading part because I'll eventually present a PCB that will include both circuits, selectable via a switch: capacitor and inductor reading and displaying results on a phone app via Bluetooth.
Most mid-range and budget testers don't have an inductor reading, and since some don't even have a capacitor reading, I combined both functions.
I tried various examples on the web with disappointing results in terms of accuracy.
I had the opportunity to test both LC and Colpitt circuits and ultimately used the Colpitt to generate a sinusoidal waveform that varies depending on the inductor and capacitors connected in the tank loop.
The result was decent right from the start, but I had to test various configurations with both transistors and ICs to find the one that best suited both small and large inductors.
In the photo below, I've shown the results of testing a 47,000 mF inductor, which I also used as a basis for calibrating the two main resistors in the circuit (R1 and R2).

As you can see, the results on the ESP32 are identical to those displayed on the oscilloscope.
To ensure a long enough duty cycle to reduce reading errors even with relatively small inductors, and to be read more accurately by the ESP32 using interrupts, you need a NON-ELECTROLYTIC capacitor of approximately 32-33 uF or greater (the higher the duty cycle, the lower the chance of error).
Unfortunately, these measurements are not available among metal-layer polyethylene ones, except in rare cases at exorbitant prices. Therefore, I created two banks with four 8 uF capacitors connected in parallel, ensuring that the total measured values ​​are similar.
High-voltage capacitors aren't needed, because the Colpitt circuit, unlike the LC circuit, doesn't generate voltage/current fluctuations from a 5V 0.5A input except at the first input until the circuit stabilizes.

In this photo, you can see three of the inductors I examined. I used the first one (marked 47mH+47mH) to calibrate the circuit with the two resistors R1-R2. Once calibrated, the inductor I made for a metal detector (number 3 in the photo) also matched the values ​​reported by my program, which I used to build it and which is based on mathematical formulas.
Number 2 in the photo is the smallest of the three; it isn't marked with values, and with my circuit, it tells me it's 10.3406 uH.
Below, you can see the 32+32 uF capacitor bank I used in the circuit.
For power, I used a 12V 1A Lipo battery and the LT3045 circuit, the photo of which is below (you can search for photos online and decide where to purchase it).

The power consumption is quite high in this Colpitt mode, and when the battery drops below 11.5V (from 12.6V at full charge), I noticed the readings change slightly. Therefore, this LT3045 circuit doesn't seem to adapt to the voltage changes. However, when the power supply is sufficient, the output is truly noise-free, so much so that you might consider powering it with a 220V>12V power supply if you're using it in a bench test and verifying that the output is equally clean.
Let's now examine the ESP32 sketch, which is derived from the one used for measuring capacitors. The routine that uses interrupts (I always used pin D4, which is more precise than other non-touch-sensitive pins) reads the time in microseconds between the first reading of the rising edge (which, as I recall, is between 2.5V and 3.3V) and the second, and then establishes the duty cycle and the resulting frequency of the input signal with the formula Frequency = 1/Duty Cycle. The inductance is then calculated with the formula:
inductance = ((float)(pow(1.0 / (6.283185307 * freq), 2)) / capacitance) / 1000.0; // in milliHenry
of which 6.283185307 is 2 times pi, freq is the frequency, and capacitance is calculated in the variable declaration at the beginning of the sketch and comes from the calculation >>> (capacitor 1 * capacitor 2) / (capacitor 1 + capacitor 2)
The two capacitor banks must be measured disconnected from the circuit, and it is essential that their capacitance reading in Farads is as accurate as possible.
Since I used capacitors that cost a few euros for 50 pieces, after placing them under load in the circuit, I removed them and measured them a second time. For this reason, it is convenient to have them inserted into an IC base and not soldered to the circuit (as you can see in the previous photo).

Calibration
The values ​​of the two resistors (R1-R2) that I reported in the LTSpice circuit (140 Ohm and 57.24 Ohm) are indicative, because everything depends on the soldering, wiring, and the transistor used. Therefore, you need to use 1K multiturn resistors for the first (R1) and a 100Ohm multiturn resistor for the second (R2). By working with these, our ESP32 should return a duty cycle similar to that resulting from the mathematical operation (6.283185307 * Sqrt(Henry * capacitance)) * 1000000.0
in which 6.283185307 is twice the pi
multiplied by the square root of the Henrys of our inductor multiplied by the capacitance, which was calculated at the beginning of the sketch as I said before on the value of the two capacitor banks.
In the zip file, I've included a small program to automatically calculate the duty cycle. Simply enter your capacitor bank values ​​and your inductor, whose value in microhenries you know, in the yellow (editable) fields. I compiled it in .NET Framework mode, Version = v4.8.1, so it works from Windows Vista onwards.
So, by looking at the value in the ESP32's serial monitor (the cycle duration) or with an oscilloscope, you can adjust R1 and R2 so that they are similar in microseconds to those resulting from the mathematical operation.
Resistor R3 is a 10K multiturn resistor; a value around 2K should be fine, but if you see an incorrect value in the serial monitor's inductance calculation, try increasing or decreasing it. This may be because output 1 of the comparator isn't sufficient to drive the ESP32's input.
To recap: to check the length of the duty cycle, it's best to use an oscilloscope connected to pin 3 of the transistor (don't connect D4 of the ESP32 to this pin because the voltage is much higher than the 3.3V supported by the ESP32). To check the comparator output, you can also check the result with the ESP32, because depending on how you adjust R3, the current will be sufficient to drive the ESP32's input or not.
The sketch performs 5 measurements and then averages the results, and as you can see from the first image, the results are identical out of 5 measurements, so I'd say it's a good result. However, some small variations can be observed when the battery voltage drops below 11.7V.

I'll soon develop the PCB that contains both the capacitor meter and the inductance meter, which can be selected with a selector switch, so that the probes are the same for the two circuits, and the power supply will only feed one of the two circuits to conserve battery power.
In the zip file, you'll find the .ino file for Esp32, the calculation file, the LTSpice file, and the images.

Hi!

Articolo Induttore.zip (802.9 KB)