Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Here's an example code using the Pololu ACS714 Current Sensor -5A to +5A with an Arduino board:
- In this code, we again define the analog input pin connected to the current sensor as currentPin. We then set the zero offset voltage and sensitivity of the sensor as zeroOffset and sensitivity, respectively. These values can be found in the sensor's datasheet.
- In the setup() function, we initialize the serial communication with a baud rate of 9600.
- In the loop() function, we first read the raw analog value from the sensor using the analogRead() function. We then convert the raw value to voltage using the formula voltage = (rawValue / 1023.0) * 5.0.
- Next, we calculate the current in amperes using the formula current = (voltage - zeroOffset) / sensitivity. We then print the raw value, voltage, and current to the serial monitor using the Serial.print() function.
- Finally, we wait for 1 second before taking another reading using the delay() function.
- */
- int currentPin = A0; // Analog input pin for current sensor
- float zeroOffset = 2.5; // Offset voltage when there is no current flow
- float sensitivity = 0.185; // Sensitivity of the current sensor in volts per ampere
- void setup() {
- Serial.begin(9600); // Initialize serial communication
- }
- void loop() {
- int rawValue = analogRead(currentPin); // Read the raw analog value from the sensor
- float voltage = (rawValue / 1023.0) * 5.0; // Convert the raw value to voltage
- float current = (voltage - zeroOffset) / sensitivity; // Calculate the current in amperes
- Serial.print("Raw Value: ");
- Serial.print(rawValue);
- Serial.print(", Voltage: ");
- Serial.print(voltage, 2);
- Serial.print(" V, Current: ");
- Serial.print(current, 2);
- Serial.println(" A");
- delay(1000); // Wait for 1 second before taking another reading
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement