## Update on Inductance Meter

It seems I may have been incorrect with my math.  The resonant frequency from the formula for a damped oscillator I was using does not match the frequency the circuit rings at.  I did improve the code a little though, by using timer1 directly instead of micros().  This significantly improves the resolution of the measurement.

//this is based on a measurement technique from
//reibot.org, the parts count has been reduced by using the avr internal comparator

double pulse, frequency, capacitance, inductance;
bool detected = false;
long timeStamp[4];
unsigned int tcnt[4];
int sample = 0;

void setup(){
Serial.begin(115200);
pinMode(11, INPUT);
pinMode(13, OUTPUT);
Serial.println("Why hello!");
delay(200);

//set up the comparator and the interrupts for it
ADCSRB = 0;           // (Disable) ACME: Analog Comparator Multiplexer Enable
ACSR =  bit (ACI)     // (Clear) Analog Comparator Interrupt Flag
| bit (ACIE)    // Analog Comparator Interrupt Enable
| bit (ACIS1);  // ACIS1, ACIS0: Analog Comparator Interrupt Mode Select (trigger on falling edge) AIN0 is D6 AIN1 is D7

//set up timer1 instead of using micros();
TCCR1A = 0; //this kills some PWM pins, but it allows timer1 to count all the way to 65535
TCCR1B = bit (CS10);

}
ISR(ANALOG_COMP_vect )
{
timeStamp[sample] = micros();
tcnt[sample] = TCNT1;
if(sample < 3)
{
sample++;
}
}
void loop(){
digitalWrite(13, HIGH);
delay(5);//give some time to charge inductor.
digitalWrite(13,LOW);

///comparator stuff here

pulse = 0;

sample = 0;
delay(500);
if(sample < 1)
{
Serial.print("time out\n");
return;
}
pulse = (tcnt[1]-tcnt[0])/16.0;
Serial.println(timeStamp[0]);
Serial.println(timeStamp[1]);
Serial.println(tcnt[0]);
Serial.println(tcnt[1]);

//end comparator stuff

capacitance = 2.2E-6; //insert capacitance here im calibrating to a known inductor, the .95 is my fudge factor.
frequency = 1.E6/(pulse);
inductance = 1./(capacitance*frequency*frequency*4.*3.14159*3.14159);
inductance *= 1E6; //note that this is the same as saying inductance = inductance*1E6
Serial.print("High for uS:");
Serial.print( pulse );
Serial.print("\tfrequency Hz:");
Serial.print( frequency );
Serial.print("\tinductance uH:");
Serial.println( inductance );
delay(20);
}



## Frequency of a Damped Oscillator (RLC Circuit)

In my last post I mentioned that the frequency of a damped oscillator is not actually the same as of an undamped oscillator.  That makes the assumptions for measuring inductance I used sadly incorrect.  I wanted to briefly run down the math on how to do the calculation correctly:

Frequency of an undamped (perfect) LC oscillator: $\omega_0=\sqrt{\frac{1}{LC}}$

Frequency of a damped oscillator:

Wikipedia page  informs us the formula for the frequency of a tank circuit taking into account the resistance of the coil is:

$\omega = \sqrt{\frac{1}{LC}-(\frac{R}{L})^2}$

I’m too lazy to solve equations since grad school.  So I typed this into my TI-89. I got:

$L=\frac{\sqrt{1-4C^2R^2\omega^2}+1}{2C\omega^2}$

In order to use this we can pulse through the resistor and measure how much voltage is dropped through the tank circuit.  At DC the capacitor is an open, so that will tell us the resistance of the inductor, which should be the only non-negligible part of this.  My R1 is 150 ohms.  I measured one of my 1/4 Watt inductors at 20 ohms.  So, I expect to measure around .6 mV, certainly not more than a volt or so.  That means I’ll do better using the internal voltage reference on the arduino.

I’ll code this up and modify the circuit and we’ll try again in the next post.

## Circuit

I recently found myself wanting to wind my own toroidal inductor.  Unfortunately I had no datasheet on the toroid cores I had purchased from Amazon: uxcell 22mm x 14mm x 8mm Power Transformer Ferrite Toroid Cores Green 10 Pcs

A while back I had tried to measure an air conditioner capacitor and ended up doing it with an Arduino, so I thought maybe a similar trick might work for inductors.  It turns out it is slightly more complicated with an inductor.  I based my work off of this blog post:  https://reibot.org/2011/07/19/measuring-inductance/

The comments stated that it may work with the built in comparator of the Arduino instead of the LM339.  My variant is as follows:

R1 is 150 ohms, but the value isn’t necessarily critical.

Powered over USB and outputs to the serial terminal.  It only takes a few minutes to put together and the only parts required are a resistor, a diode, and a 2.2 uF cap.  You can use another value if you want and the range of measurement will change.  You will also have to edit the source code to match.

## Code

 //this is based on a measurement technique from //reibot.org, the parts count has been reduced by using the avr internal comparator

double pulse, frequency, capacitance, inductance;
bool detected = false;
long timeStamp[4];
int sample = 0;
void setup(){
Serial.begin(115200);
pinMode(11, INPUT);
pinMode(13, OUTPUT);
Serial.println(“Why hello!”);
delay(200);
ADCSRB = 0; // (Disable) ACME: Analog Comparator Multiplexer Enable
ACSR = bit (ACI) // (Clear) Analog Comparator Interrupt Flag
| bit (ACIE) // Analog Comparator Interrupt Enable
| bit (ACIS1); // ACIS1, ACIS0: Analog Comparator Interrupt Mode Select (trigger on falling edge) AIN0 is D6 AIN1 is D7

}
ISR(ANALOG_COMP_vect )
{
timeStamp[sample] = micros();
if(sample < 3)
{
sample++;
}
}
void loop(){
digitalWrite(13, HIGH);
delay(5);//give some time to charge inductor.
digitalWrite(13,LOW);

///comparator stuff here

pulse = 0;

sample = 0;
delay(500);
if(sample < 2)
{
Serial.print(“time out\n”);
return;
}
pulse = (timeStamp[1]-timeStamp[0]);

//end comparator stuff

capacitance = 2.2E-6*.92; //insert capacitance here im calibrating to a known inductor, the .95 is my fudge factor.
frequency = 1.E6/(pulse);
inductance = 1./(capacitance*frequency*frequency*4.*3.14159*3.14159);
inductance *= 1E6; //note that this is the same as saying inductance = inductance*1E6
Serial.print(“High for uS:”);
Serial.print( pulse );
Serial.print(“\tfrequency Hz:”);
Serial.print( frequency );
Serial.print(“\tinductance uH:”);
Serial.println( inductance );
delay(20);
}

## Theory

The capacitor and the inductor in parallel form a resonant circuit.  When a pulse of current goes through this, part of the energy goes into making the circuit oscillate or “ring”.  I took a photo of what this looks like on my low cost oscilloscope:

The code sends a pulse through the resonant circuit.  Using the comparator interrupt, it gets a time stamp with micros() on the next 3 falling edges.  The time difference between the first 2 falling edges is one period, or 1/f.  Using the formula for resonance of a harmonic oscillator it calculates the inductance.

The blog post I used as a reference for this incorrectly states that the frequency stays the same regardless of the resistance of the inductor.  Unfortunately, a damped harmonic oscillator does not resonate at the same frequency as a perfect one.  If there is any interest I might make a version of this circuit that measures resistance first and corrects for this.  Otherwise this is close enough for most purposes, especially if you have a good, low resistance, inductor.

## Tesla Coil Build

I’d like to walk through the build of a musical tesla coil.  Over all I would give this kit a 9 out of 10 rating.

The instructions were only in Chinese, but the schematic and the image were clear enough that I got it built anyway.  I put a link to purchase a very similar kit on amazon below.  I hope you’ll give it a go.  You can also find the kit on aliexpress for significantly cheaper if you are patient and cheap.

Here is a photo of the schematic it came with:

I buzzed out some points and found the schematic is actually completely wrong though. For one, LED1 isn’t there, it goes from the base of Q2 to ground.  I’m pretty sure it is supposed to go the other way as well, to protect the base from large negative voltages.  I tried to create a correct schematic but I should have started before assembly. The board is pretty simple, but with all the parts soldered on already it’s a bit hard to create the correct schematic.

This is the finished item. The coil just sits there, so you will want to glue it on with something. Over all it’s a great kit and it really plays sound, though somewhat terribly.  Plasma speakers are usually only used for the high end though, so you might actually integrate one of these into a speaker box as a tweeter for fun.

The main problems were the instructions, and the schematic being incorrect.  It came with a small neon bulb you can watch light up when you hold it close.  That was definitely fun to play with. It does burn about 15 Watts for almost no sound output, but it’s a novelty anyway.

You can see the arc at the top pretty clearly.  This has really got me thinking and I think I’m going to try to construct a small tesla coil without the kit at some point just to understand the schematic better.

Check out my video as well:

## Negative volts in arduino

### The simplest charge pump

If you read my last post you know how to get fast PWM off pin 11 (or pin 3) without messing up your millis() and delay() functions.  Keep in mind this is still using up one of your timers, so pin 3 and 11 should not be used for analogWrite() if you use this method.

I promised a quick and easy answer to the question “how do I generate a negative voltage.  Here it is:

Use cheap electrolytics, and run PWM with about 50% duty cycle on pin 11.  You will only be able to pull a few milliamps off your negative voltage, but that can be enough for some things.

### How it works

The square wave is passed through the C1.  When output is VCC, the other side of C1 grounds through D1.  The voltage at the negative terminal of C1 is now equal to the diode drop (.7 volts for the shown 1N4004).  Pin 11 goes to ground, and the voltage at the negative terminal is reduced by 5 volts.  it is now -5+.7=4.3 V.  This is pulled through D2, and charges C2.  The voltage across C2 will be -5 V + twice the diode drop.  That means for the configuration shown it will be about -3.6 V.  The big problem with this is the 1.4 V drop through the two diodes.  We can reduce that by using Schottky diodes, which will only drop around half the voltage.

## Arduino Fast PWM

The usual way to do PWM on an arduino is using analogWrite().  This works for a lot of purposes, but is very slow.  PWM done this way operates around 500 Hz.  That’s fine if you are dimming a single LED, but PWM can be used for so much more.

Common uses for PWM include:

• motor speed controllers
• switch mode power supplies
• waveform generation, including audio output

There are arduino libraries available for some of these, and shields and modules for others.  But it’s worth knowing how to do fast PWM anyway.

On any of the mega88/168/328 based arduinos (I like to use nanos) the following simple sketch will work:

 void setup() { // put your setup code here, to run once: pinMode(11, OUTPUT); TCCR2A = _BV(COM2A1) | _BV(WGM21) | _BV(WGM20); TCCR2B = _BV(CS20); //see table for clock select OCR2A = 180; //this value from 0-255 is the duty cycle }
 void loop() { // put your main code here, to run repeatedly: OCR2A = (OCR2A+1)%256; delay(10); }

This outputs PWM on digital pin 11 at 62500 Hz.  There are much more detailed instructions here: https://www.arduino.cc/en/Tutorial/SecretsOfArduinoPWM, but I’m going somewhere with this.  I noticed a question the other day.  It was “How do I make a negative voltage with an arduino?”.  In my next post I’ll have two answers to this, one with inductors, diodes, and capacitors, and one with only capacitors and diodes.  That is going to lead into another post and video about fundamentals of switch mode power supplies.  That can be a daunting thing to get into, but once we experiment a little you’ll find all sorts of uses in your projects.