I'm developing a piece of test equipment using an Arduino MEGA 2560 interfacing with a 4D Systems uLCD-70-DT resistive touchscreen display. The display allows for the user to input required information, and the Arduino acts as a host to process and return the inputs to display on the screen.
Accepting and displaying user inputs is the first step in my software design. This was a success. However, now I need the Arduino to read and monitor AC current values using a non-invasive current sensor.
I found an Arduino library capable of doing this courtesy of OpenEnergyMonitor.org. Applying this library, and implementing the following code is what allows for the Arduino to read and calculate the current that the sensor is seeing.
#include "EmonLib.h" // Include Emon Library
EnergyMonitor emon1; // Create an instance
void setup()
{
Serial.begin(9600);
emon1.current(5, 60); // Current: input pin, calibration.
//calibration is explained bellow
}
void loop()
{
double Irms = emon1.calcIrms(1480); // Calculate Irms only
Serial.print(Irms*230.0); // Apparent power
Serial.print(" ");
Serial.println(Irms); // Irms
}
The following is my code, made to process events from the display, and now with the current sensor:
void setup()
{
Serial.begin(115200); // baud rate for the Arduino AND THE DISPLAY is set at 115200
Serial1.begin(115200); // to comminicate with LCD
genie.Begin(Serial1); // Serial port 1 (RX1 & TX1 on the Arduino) handles user keyboard inputs
genie.AttachEventHandler(myGenieEventHandler);
pinMode(RESETLINE, OUTPUT); // Set D4 on Arduino to Output (4D Arduino Adaptor V2 - Display Reset)
digitalWrite(RESETLINE, 1); // Reset the Display via D4
delay(100);
digitalWrite(RESETLINE, 0); // unReset the Display via D4
emon1.current(5, 64.3); // Current: input pin, calibration.
delay (3500); //let the display start up after the reset (This is important)
}
void loop()
{
//Process events
genie.DoEvents();
keyvalueConvc = atof(keyvaluec); // ascii to float conversion for copper inputs
keyvalueConva = atof(keyvaluea); // ascii to float conversion for aluminum inputs
/* physical computing for the current sensor:
* i(measured) = √2 * i(rms_current) = 1.414 * 150A = 212.1 A
* The current at the output of the sensor is defined by its number of turns, which is 2125 turns.
* i(sensor) = i(measured) / nb_turns = 212.1A / 2125 = 0.0998A
* Arduino can only handle voltage (between 0V and 5V) and so the current needs to be converted into an acceptable voltage. So a burden resistor is needed in the circuit.
* As the current is alternative around 0 and to maximize measurement resolution, the max voltage at burden resistance should be Max_accepted_voltage / 2 = 2.5V.
* Therefore, the best burden resistor value is
* R(burden) = U(sensor)/I(sensor) = 2.5V / 0.0707A = 35.4Ω
* A 33Ω resistor will be used in this case.
* Arduino can not measure negative voltage, so 2.5V is added to U(sensor) to make the voltage measurable (between 0V and 5V).
* Additionally, 2 resistors are added, 10kΩ to avoid too much energy consumption.
* Also, a low reactance capacitor, 10uF – a few hundred ohms – is added and provides an alternative path for the alternating current to bypass the resistor.
* Finally, the current sensor needs calibration:
* calibration_value = ( i(measured) / i(sensor) ) / R(burden)
* calibration_value = (212.1 A / 0.0998A) / 33Ω
* calibration_value = 2125/33Ω = 64.3
*/
double Irms = emon1.calcIrms(1480); // Calculate Irms based on current sensor
}
The Arduino now does not return and show user inputs on the display. The one line of code
double Irms = emon1.calcIrms(1480); // Calculate Irms based on current sensor
is what breaks this function.
My theory is that the display and the current calculation are both contending for the serial communication. Thus, user inputs are no longer being processed and returned to the display because it also intends to receive and calculate information from the current sensor.
If I am right, is there a method to achieve simultaneous serial communications? If I am wrong, does anyone see any issues that would cause my prototype to lose this functionality?
Attached is my complete sketch if it is of any assistance.
Thanks for any time and help.
Sketch_RevA.ino (10.3 KB)