You are on page 1of 11

// Tube Tester for measuring Va,Ia, Vg, Gr, Rp, Mu

// by Alessandro Galavotti -

// Last Revision Dec. 12 - 2020

#include <Wire.h>

#include <Adafruit_ADS1015.h>

Adafruit_ADS1115 ads(0x48); // Address of ADS115

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2); // Addres of ic2 LCD Display

// use debug=true to have continuous readings on serial monitor and calculate the scale factors

bool Debug =true;

float Voltage0 = 0.0; // AD outputs

float Voltage1 = 0.0;

float Voltage2 = 0.0;

float Voltage3 = 0.0;

float ScaleV0 =(250.8/2.724); // scale factor for V0 (Anode Voltage)

float ScaleV1 =(7.503/0.0808125); // scale factor for V1 (Filament Voltage)

float ScaleV2 =0.5; // scale factor for V2 (Anode Current) Sense Resistor=2ohm -> 1/2=0.5

float ScaleV3 =(20.029/0.4891875);// scale factor for V3 (Grid Voltage)

float Va,Ia,Vg,Vf;

float Vam,Iam,Vgm,Vfm; //medium values

float Va1,Ia1,Vg1;

float Va2,Ia2,Vg2;

float Va3,Ia3,Vg3;

float Mu,Gm,Rp;

int nMeasure =1; // progressive number of test

int nSample =10; // number of measured data averaged


const int buttonPin1 = 12; // the number of the pushbutton pin

const int ledPin1 = 11; // the number of the LED pin

int buttonState1 = 0;

const int buttonPin2 = 6; // the number of the pushbutton pin

const int ledPin2 = 10; // the number of the LED pin

int buttonState2 = 0;

const int buttonPin3 = 2; // the number of the pushbutton pin

int buttonState3 = 0;

//const int ledPin2 = 10; // the number of the LED pin

// Setup ******

void setup(void)

lcd.init();

Serial.begin(9600);

ads.begin();

// initialize the LED pin as an output:

pinMode(ledPin1, OUTPUT);

// initialize the pushbutton pin as an input:

pinMode(buttonPin1, INPUT);

pinMode(ledPin2, OUTPUT);

// initialize the pushbutton pin as an input:

pinMode(buttonPin2, INPUT);

pinMode(buttonPin3, INPUT);

// Main Program *****


void loop(void)

// Do ADC conversion

DoADC();

// ***** Output to serial LCD

lcd.setCursor(0, 0);

lcd.backlight();

lcd.print("Va");

lcd.print(Va);

if ( Voltage0 <100 ) { // per annullare una cifra che resta quando Va<100V

lcd.setCursor(7, 0);

lcd.print(" ");

lcd.setCursor(9, 0);

lcd.print("Ia");

lcd.print(Ia);

if ( Ia <10 ) { // per annullare una cifra che resta quando Ia<10ma

lcd.setCursor(15, 0);

lcd.print(" ");

lcd.setCursor(0, 1);

lcd.print("Vg");

lcd.print(Vg);

lcd.setCursor(9, 1);

lcd.print("Vf");

lcd.print(Vf);
//

//*** Set Measure Point for Rp, Gm, Mu calculation ***

// Measure Point P0

buttonState1 = digitalRead(buttonPin1);

if (buttonState1 == LOW) {

// turn LED on:

digitalWrite(ledPin1, HIGH);

Ia1=Ia;

Va1=Va;

Vg1=Vg;

clearscreen(); // istruction for next point

lcd.setCursor(0, 0);

lcd.print("P1=decr.Vg@VAfix");

lcd.setCursor(0, 1);

lcd.print("Then press P2");

WaitSec(9);

clearscreen();

} else {

// turn LED off:

digitalWrite(ledPin1, LOW);

// Measure Point P2

buttonState2 = digitalRead(buttonPin2);

if (buttonState2 == LOW) {

// turn LED on:

digitalWrite(ledPin2, HIGH);

Ia2=Ia;
Va2=Va;

Vg2=Vg;

clearscreen(); // istructions for next point

lcd.setCursor(0, 0);

lcd.print("P2=incr.VA@Vgfix");

lcd.setCursor(0, 1);

lcd.print("Then Press P3");

WaitSec(9);

clearscreen();

} else {

// turn LED off:

digitalWrite(ledPin2, LOW);

// Measure Point P3 and calculate Rp. Gm, Mu

buttonState3 = digitalRead(buttonPin3);

if (buttonState3 == LOW) {

// turn LED on:

digitalWrite(ledPin2, HIGH);

digitalWrite(ledPin1, HIGH);

Ia3=Ia;

Va3=Va;

Vg3=Vg;

clearscreen();

Gm=(Ia2-Ia1)/(Vg2-Vg1);

Rp=(Va3-Va2)/(Ia3-Ia2);

Mu=Gm*Rp;
lcd.setCursor(0, 0);

lcd.print("Gm=");

lcd.print(Gm);

lcd.setCursor(7, 0);

lcd.print("Rp=");

lcd.print(Rp);

lcd.setCursor(0, 1);

lcd.print("Mu=");

lcd.print(Mu);

lcd.setCursor(10, 1);

lcd.print("#");

lcd.print(nMeasure);

// tabulate data and resutl on serial monitor

Serial.print("-----------------------------------------");

Serial.println();

Serial.print("Measure#:");

Serial.print(nMeasure);

Serial.println();

Serial.print("Va:");

Serial.print("\t\tVg:");

Serial.print("\t\tIa:");

Serial.println();

Serial.print(Va1, 7);

Serial.print("\t");

Serial.print(Vg1, 7);

Serial.print("\t");

Serial.print(Ia1, 7);

Serial.println();

Serial.print(Va2, 7);
Serial.print("\t");

Serial.print(Vg2, 7);

Serial.print("\t");

Serial.print(Ia2, 7);

Serial.println();

Serial.print(Va3, 7);

Serial.print("\t");

Serial.print(Vg3, 7);

Serial.print("\t");

Serial.print(Ia3, 7);

Serial.println();

Serial.print("Gm:");

Serial.print("\t\tRp:");

Serial.print("\t\tMu:");

Serial.println();

Serial.print(Gm, 7);

Serial.print("\t");

Serial.print(Rp, 7);

Serial.print("\t");

Serial.print(Mu, 7);

Serial.println();

WaitSec(9);

clearscreen();

nMeasure=nMeasure+1;

} else {

// turn LED off:

digitalWrite(ledPin2, LOW);

digitalWrite(ledPin1, LOW);

}
delay(200);

// ******** SUBs ***********

void clearscreen (void){

for (int r = 0; r <=1; r++) {

for (int c = 0; c <=15; c++) {

lcd.setCursor(c, r);

lcd.print(" ");

lcd.setCursor(0, 0);

void WaitSec (int i){

for (int ii = i; ii>0; ii--) {

lcd.setCursor(14, 1);

lcd.print(ii);

delay(1000);

void DoADC() {

// do ADC NConv Times, mediate and returns Voltage0

int16_t adc0; // we read from the ADC, we have a sixteen bit integer as a result

int16_t adc1; // we read from the ADC, we have a sixteen bit integer as a result

int16_t adc2; // we read from the ADC, we have a sixteen bit integer as a result

int16_t adc3; // we read from the ADC, we have a sixteen bit integer as a result
Vam=0;

Iam=0;

Vgm=0;

Vfm=0;

// mediate values nSample times

for ( int kk=1; kk<=nSample; kk++) {

// **** READ ADC *****

// ADC0 - Anode Voltage

adc0 = ads.readADC_SingleEnded(0);

Voltage0 = (adc0 * 0.1875)/1000;

Va=Voltage0*ScaleV0; // Anode Voltage

// ADC1 - Filament Voltage

adc1 = ads.readADC_SingleEnded(1);

Voltage1 = (adc1 * 0.1875)/1000;

if ( Voltage1 <0 ) {

Voltage1 =0 ;

Vf=Voltage1*ScaleV1; // partitore V0 (fil)

// ADC2 - Anode Current

adc2 = ads.readADC_SingleEnded(2);

Voltage2 = (adc2 * 0.1875)/1000;

if ( Voltage2 <0 ) {

Voltage2 =0 ;

Ia=Voltage2*ScaleV2*1000; // Ia in mA

// ADC3
adc3 = ads.readADC_SingleEnded(3);

Voltage3 = (adc3 * 0.1875)/1000;

Vg=-Voltage3*ScaleV3; // partitore V3 (vg)

// little correction of Va and Vg for drop on Cathode Resistor

Va=Va-Voltage2;

Vg=Vg+Voltage2;

// cumulate for average

Iam=Iam+Ia;

Vam=Vam+Va;

Vgm=Vgm+Vg;

Vfm=Vfm+Vf;

Ia=Iam/nSample;

Va=Vam/nSample;

Vg=Vgm/nSample;

Vf=Vfm/nSample;

// printing to serial moniotr data readed and converted (for debug and scale factor extimate)

if ( Debug ) { // for scale factor estimate

Serial.print("\t Voltage0/Va: ");

Serial.print(Voltage0, 7);

Serial.print("\t Voltage1/Vf: ");

Serial.print(Voltage1, 7);

Serial.print("\t Voltage2/Ia: ");

Serial.print(Voltage2, 7);

Serial.print("\t Voltage3/Vg: ");

Serial.print(Voltage3, 7);

Serial.println();

}
//if ( Debug = 100 ) { // for raw data

// Serial.print("AIN0: ");

// Serial.print(adc0, 7);

// Serial.print("AIN1: ");

// Serial.print(adc1, 7);

// Serial.print("AIN2: ");

// Serial.print(adc2, 7);

// Serial.print("AIN3: ");

// Serial.print(adc3, 7);

// }

You might also like