Advertisement
microrobotics

Pololu ACS714 Current Sensor -5A to +5A

Apr 21st, 2023
610
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Here's an example code using the Pololu ACS714 Current Sensor -5A to +5A with an Arduino board:
  3.  
  4. 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.
  5.  
  6. In the setup() function, we initialize the serial communication with a baud rate of 9600.
  7.  
  8. 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.
  9.  
  10. 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.
  11.  
  12. Finally, we wait for 1 second before taking another reading using the delay() function.
  13. */
  14.  
  15. int currentPin = A0; // Analog input pin for current sensor
  16. float zeroOffset = 2.5; // Offset voltage when there is no current flow
  17. float sensitivity = 0.185; // Sensitivity of the current sensor in volts per ampere
  18.  
  19. void setup() {
  20.   Serial.begin(9600); // Initialize serial communication
  21. }
  22.  
  23. void loop() {
  24.   int rawValue = analogRead(currentPin); // Read the raw analog value from the sensor
  25.   float voltage = (rawValue / 1023.0) * 5.0; // Convert the raw value to voltage
  26.   float current = (voltage - zeroOffset) / sensitivity; // Calculate the current in amperes
  27.   Serial.print("Raw Value: ");
  28.   Serial.print(rawValue);
  29.   Serial.print(", Voltage: ");
  30.   Serial.print(voltage, 2);
  31.   Serial.print(" V, Current: ");
  32.   Serial.print(current, 2);
  33.   Serial.println(" A");
  34.   delay(1000); // Wait for 1 second before taking another reading
  35. }
  36.  
  37.  
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement