Maker Pro
Maker Pro

Help with arduino code

I have found a project on instructibles that measures capacitance. However, you have to use the serial monitor to see the values measured. I want to show these results on a 1602 lcd. Can this be done?
 

Harald Kapp

Moderator
Moderator
O.K., I know that answer is technically correct but doesn't help you.;)

Depending on how the code for outputting the result is structured, it may be as simple as replacing the calls to the serial output routine by calls to an equivalent LCD routine (there should be a library for this type of LCD).
You probably will have to take care for the positioning of the write cursor (the location where the text is displayed) as this would not be handled by the serial routines.
 
Thanks Harald. Here is the code I am using:

Code:
// Initialize Pins
int analogPin = 0;
int chargePin = 13;
int dischargePin = 11; //speeds up discharging process, not necessary though

// Initialize Resistor
int resistorValue = 10000;

// Initialize Timer
unsigned long startTime;
unsigned long elapsedTime;

// Initialize Capacitance Variables
float microFarads;               
float nanoFarads;

void setup()
{
  pinMode(chargePin, OUTPUT);    
  digitalWrite(chargePin, LOW); 
  Serial.begin(9600); // Necessary to print data to serial monitor over USB
}

void loop()
{
  digitalWrite(chargePin, HIGH); // Begins charging the capacitor
  startTime = millis(); // Begins the timer
 
  while(analogRead(analogPin) < 648)
  {      
    // Does nothing until capacitor reaches 63.2% of total voltage
  }

  elapsedTime= millis() - startTime; // Determines how much time it took to charge capacitor
  microFarads = ((float)elapsedTime / resistorValue) * 1000;
  Serial.print(elapsedTime);      
  Serial.print(" mS    ");        

  if (microFarads > 1) // Determines if units should be micro or nano and prints accordingly
  {
    Serial.print((long)microFarads);      
    Serial.println(" microFarads");        
  }

  else
  {
    nanoFarads = microFarads * 1000.0;     
    Serial.print((long)nanoFarads);        
    Serial.println(" nanoFarads");         
    delay(500);
  }

  digitalWrite(chargePin, LOW); // Stops charging capacitor
  pinMode(dischargePin, OUTPUT);
  digitalWrite(dischargePin, LOW); // Allows capacitor to discharge   
  while(analogRead(analogPin) > 0)
  {
    // Do nothing until capacitor is discharged     
  }

  pinMode(dischargePin, INPUT); // Prevents capacitor from discharging 
}

I have both I2C and serial versions of the LCD1602. I prefer to use the I2C but serial will be ok too. However, my goal is to use a Pro Mini or Pro Micro for this project as I want to place this into an enclosure.

What do I need to do to implement the LCD?
 

Harald Kapp

Moderator
Moderator
O.K.,
as I suspected the code plainly sends the results via serial.print to the serial port without any formatting.
What you need is a library for controlling this kind of LCD. I'm sure you can find one on the internet (e.g. here or here - the latter one is in German but Google translate can help you. Or look for another source in English). Otherwise you'd have to do the coding completely from scratch which will take a bit more time and a deeper understanding of the LCD.
You will also have to take care of the correct connection of the LCD to the arduino and you may have to set some defines in the code accordingly (both are explained in the German link).
What you have to do is replace the serial.print statements by calls to the LCD library telling the LCD to display the data. As data to an LCD is typically written at the position of a cursor, you will have to place the cursor to the start of the LCD's screen before outputting new data (over a serial connection the receiving program or terminal emulator will do this for you, here you'll have to do it yourself).
 
Top