Hardware Reference
In-Depth Information
Testing the project
We can now write a simple test for the project, just to see if the hardware connections are
correct. We are going to write the code inside the Arduino IDE. Note that this section ex-
plains the code for testing the project, and that can you find the complete code in the
GitHub repository of the project.
The code starts by defining the pin of the current sensor:
#define CURRENT_SENSOR A0
We then define the different variables that are necessary for the test. Here, we are going to
measure the current and the power. Note that we don't measure the voltage of the device, so
you will have to define it yourself depending on your country:
float amplitude_current;
float effective_value;
float effective_voltage = 230; // Set voltage to 230V
(Europe) or 110V (US)
float effective_power;
float zero_sensor;
Then, in the setup() function of the sketch, we need to get the zero value of the sensor.
As we have an analog sensor, we need to know what the sensor is reading when no current
is flowing:
zero_sensor = getSensorValue();
Serial.print("Zero point sensor: ");
Serial.println(zero_sensor);
Serial.println("");
Let's now look at the details of this function that measures the value from the sensor. As it's
an analog sensor, we need to average the readings over several measurements to get a
stable measurement. For this project, the value is averaged over 100 measurements. This is
done by the following function:
float getSensorValue()
{
int sensorValue;
Search WWH ::




Custom Search