Advertisement
microrobotics

ACS758 Hall effect current sensor

Mar 31st, 2023
1,281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. The ACS758 is a Hall-effect-based linear current sensor that provides an analog output voltage proportional to the current flowing through the device. Here's a simple Arduino code to read the sensor's output and display the current on the Serial Monitor:
  3.  
  4. To wire the ACS758 sensor, connect its VIOUT (voltage output) pin to the ACS758_PIN (analog pin A0) on the Arduino. Connect the VCC pin to the 5V pin on the Arduino and the GND pin to the ground.
  5.  
  6. This code will continuously read the sensor's output and print the raw readings, the corresponding voltage, and the calculated current on the Serial Monitor. Make sure to adjust the SENSITIVITY and ZERO_BIAS_VOLTAGE constants according to your specific ACS758 sensor model.
  7.  
  8. Note that the ACS758 is a unidirectional current sensor, meaning it can only measure current in one direction. If you need to measure bidirectional currents, consider using an ACS758 bidirectional sensor and adjust the code accordingly.
  9. */
  10.  
  11.  
  12. const int ACS758_PIN = A0; // ACS758 sensor connected to analog pin A0
  13.  
  14. // Sensor parameters (change these according to your specific sensor model)
  15. const float VOLTAGE_REF = 5.0;
  16. const float SENSITIVITY = 0.040; // Sensitivity in V/A (40 mV/A for ACS758LCB-100U)
  17. const float ZERO_BIAS_VOLTAGE = VOLTAGE_REF / 2; // Zero current output voltage (typically Vcc/2)
  18.  
  19. void setup() {
  20.   Serial.begin(9600);
  21. }
  22.  
  23. void loop() {
  24.   int sensorValue = analogRead(ACS758_PIN);
  25.  
  26.   // Convert the analog reading to voltage
  27.   float voltage = sensorValue * (VOLTAGE_REF / 1023.0);
  28.  
  29.   // Calculate the current
  30.   float current = (voltage - ZERO_BIAS_VOLTAGE) / SENSITIVITY;
  31.  
  32.   Serial.print("Sensor Value: ");
  33.   Serial.println(sensorValue);
  34.   Serial.print("Voltage: ");
  35.   Serial.print(voltage);
  36.   Serial.println(" V");
  37.   Serial.print("Current: ");
  38.   Serial.print(current);
  39.   Serial.println(" A");
  40.  
  41.   delay(1000); // Wait 1 second between readings
  42. }
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement