The idea for this project is to simulate the signals going to the relays that activate a capacitance bank. I am using an Arduino DUE and the Adafruit-LCD-Keypad_Board (with I2C).
The Arduino receives two inputs: the inductance value L that will be used and the desired resonance frequency f. The Arduino calculates the capacitance value that has to be used to obtain the desired frequency, using the formula:
![](https://mstronik.blog/wp-content/uploads/2019/09/c_formula.jpg?w=308)
Because this capacitance value is theoretical, the Arduino makes an approximation for the calculated capacitance value using the actual available capacitance values. These values are:
1nF, 2.2nF, 4.7nF, 10nF, 22nF, 47nF, 100nF and 200nF
Initial menu:
![](https://mstronik.blog/wp-content/uploads/2019/09/menu1.jpg?w=779)
Select the inductance value to be used, by pressing the LEFT Button ( to decrease the value) or the RIGHT Button ( to increase the value) on the Keypad:
![](https://mstronik.blog/wp-content/uploads/2019/09/menu1_select.jpg?w=782)
Once you have chosen the value, press the SELECT Button:
![](https://mstronik.blog/wp-content/uploads/2019/09/menu1_ready.jpg?w=779)
The display will automatically change to the next menu and will ask for the desired resonance frequency:
![](https://mstronik.blog/wp-content/uploads/2019/09/menu2_f-1.jpg)
Select the desired frequency using the LEFT Button or the RIGHT Button and then press the SELECT Button:
![](https://mstronik.blog/wp-content/uploads/2019/09/menu2_f_select.jpg?w=779)
The Arduino will calculate and display automatically the calculated capacitance value, using the formula:
![](https://mstronik.blog/wp-content/uploads/2019/09/c_formula.jpg?w=308)
![](https://mstronik.blog/wp-content/uploads/2019/09/c_value_theory.jpg)
When you press the SELECT button, the Arduino will calculate the possible capacitance value that can be implemented using the existing values on the board. The Arduino will then turn on the corresponding relays (here simulated with LEDs) for the capacitance value that needs to be implemented:
![](https://mstronik.blog/wp-content/uploads/2019/09/c_value_real.jpg)
LEDs corresponding to the 1nF, 4.7nF and 22nF are turned on.
1 nF + 4.7 nF + 22 nF = 27.70 nF (theoretical calculated value was 27.92 nF)
MATERIALS:
Arduino DUE: https://www.arduino.cc/en/Guide/ArduinoDue
Adafruit LCD Shield: https://www.adafruit.com/product/714
You will need to include the following Libraries:
From Arduino:
wire.h for serial communication.
If using the Adafruit-LCD-Keypad_Board (with I2C IC), you will need the Adafruit-RGB-LCD-Shield-Library:
Download the code for the Arduino DUE at GitHub:
https://github.com/Magdalena73/LCD_Keypad_CapValue
/*** Program for the blog: ***
** https://mstronik.blog/2019/09/17/lcd-keypad_menu-to-obtain-capacitance-value/ **
** The menu asks for the desired frequency and used inductance to calculate the required capacitance **
** aproximating to the implemented values of the capacitor bank **
** Magdalena Vaclavek **/
// include the library code:
#include <Adafruit_RGBLCDShield.h>
#include <Wire.h>
//#include <utility/Adafruit_MCP23017.h>
#define ON 0x7
#define OFF 0x0
#define num_Screens 4
float C_bank[8] = {1.0, 2.2, 4.7, 10.0, 22.0, 47.0, 100.0, 200.0};
float C_array[256];
String screens[num_Screens][2] = {{"Inductance","uH"}, {"Frequency", "kHz"},{"Capacitance is","nF"},{"Implemented C = ","nF"}};
float parameters[num_Screens];
int currentScreen,newScreen;
bool flag_Screen;
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
//******************* generate C_bank possible values *********************
void generate_array(float C_array[256]){
int i, j, counter, bit_counter; float C_value;
counter = 1;
for(j = 0; j < 256; j++){
C_array[j] = 0.0;
for (i = 0; i < 8; i++){
bit_counter = (counter >> i) &1;
if(bit_counter == 1){
C_array[j] = C_array[j] + C_bank[i];
}
}
counter++;
}
}
void setup() {
generate_array(C_array);
//initialize the ports as outputs, for the relais:
for(int i= 2; i< 10; i++){
pinMode(i,OUTPUT);
}
//initialize all relais OFF
for(int i= 2; i< 10; i++){
digitalWrite(i,LOW);
}
//Serial.begin(9600);
//initialize LCD:
lcd.begin(16, 2);
lcd.setCursor(0,0);
parameters[0] = 100.0;
parameters[1] = 100.0;
parameters[2]= 0.0;
parameters[3] = 0.0;
currentScreen = 0;
printScreen();
lcd.setBacklight(ON);
flag_Screen = true;
}
// ***************** MAIN ******************************
void loop() {
uint8_t buttons = lcd.readButtons();
// if (flag_Screen == true)){
if (buttons) {
if (buttons & BUTTON_UP) {
screenChange(0);
}
if (buttons & BUTTON_DOWN) {
screenChange(1);
}
if (buttons & BUTTON_LEFT) {
parameterChange(1);
}
if (buttons & BUTTON_RIGHT) {
parameterChange(0);
}
if (buttons & BUTTON_SELECT) {
storeValues();
}
/* printScreen();
delay(100);*/
}
if (newScreen != currentScreen){
printScreen();
delay(100);
newScreen = currentScreen;
}
}
//****************** functions ******************************************
void screenChange(int input){
if(input == 0){ // 0 = bttn UP, last menu
if (currentScreen == 0){
currentScreen = num_Screens - 1;
}
else{
currentScreen--;
}
}
else if (input == 1){ // 1 = btnn DOWN, next menu
if(currentScreen == num_Screens - 1){
currentScreen = 0;
}
else{
currentScreen++;
}
}
}
//********************************************************
void parameterChange(int key){
if ((currentScreen == 0)||(currentScreen == 1)){
if(key == 0){ // 0 = bttn RIGHT, increase the parameter
parameters[currentScreen]= parameters[currentScreen] + 5.0;
}
else if(key == 1){ // 1 = bttn LEFT, decrease the parameter
parameters[currentScreen]= parameters[currentScreen] - 5.0;
}
}
printScreen();
delay(100);
}
//***************************************************
void storeValues()
{
float L_value,freq, w, C_value,C_implemented;
int C_index;
switch (currentScreen){
case 0: {
for(int i= 2; i< 10; i++){
digitalWrite(i,LOW);
}
currentScreen = 1;
break;
}
case 1: {
L_value = parameters[0];
freq = parameters[1];
w = 2.0*PI*freq;
C_value = 1000000000.0/(w*w*L_value);
parameters[2] = C_value;
currentScreen = 2;
break;
}
case 2: {
L_value = parameters[0];
freq = parameters[1];
w = 2.0*PI*freq;
C_value = 1000000000.0/(w*w*L_value);
parameters[2] = C_value;
C_index = found_value(C_value);
C_implemented = C_array[C_index];
parameters[3] = C_implemented;
C_index = C_index+1;
C_turn_on(C_index);
/*printScreen();
delay(100);*/
currentScreen = 3;
break;
}
case 3:{
C_value = parameters[2];
C_index = found_value(C_value);
C_implemented = C_array[C_index];
parameters[3] = C_implemented;
C_index = C_index+1;
C_turn_on(C_index);
currentScreen = 0;
break;
}
}
}
//********************************************
void printScreen() {
lcd.clear();
lcd.print(screens[currentScreen][0]);
lcd.setCursor(0,1);
lcd.print(parameters[currentScreen]);
lcd.setCursor(7,1);
lcd.print(screens[currentScreen][1]);
}
//*********************************************
int found_value (float value){
int i, index;
for(i = 0; i < 256; i++){
if(value >= C_array[i]){
index = i;
}
else
i = 256;
}
return(index);
}
//*******************************
void C_turn_on(int counter){
int bit_counter, i, pin_nr;
for (i = 0; i < 8; i++){
pin_nr = i+2;
bit_counter = (counter >> i) &1;
if(bit_counter == 1){
digitalWrite(pin_nr,HIGH);
}
else{
digitalWrite(pin_nr,LOW);
}
}
}