Advertisement
microrobotics

DF Robot Analog Rotation Sensor V2

Mar 30th, 2023
632
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*The DF Robot Analog Rotation Sensor V2 is a potentiometer-based rotary sensor that measures the rotation of a shaft. Here's an example code in Arduino that reads the rotation angle from the sensor:
  2.  
  3. Note: This code assumes that the DF Robot Analog Rotation Sensor V2 is connected to pin A0 of the Arduino board. If you're using a different pin, you'll need to modify the SENSOR_PIN definition accordingly. Also, the voltage range of the sensor is assumed to be 0.5V to 4.5V, which may vary between sensors. If you need a more accurate measurement, you can calibrate the sensor using a known angle.
  4. */
  5.  
  6.  
  7. const int SENSOR_PIN = A0;  // the pin connected to the sensor
  8. float voltage;  // the voltage reading from the sensor
  9. float angle;  // the angle calculated from the voltage
  10.  
  11. void setup() {
  12.   Serial.begin(9600);
  13.  
  14.   pinMode(SENSOR_PIN, INPUT);
  15. }
  16.  
  17. void loop() {
  18.   // read the voltage from the sensor
  19.   voltage = analogRead(SENSOR_PIN) * (5.0 / 1023.0);
  20.  
  21.   // calculate the rotation angle in degrees
  22.   angle = (voltage - 0.5) * 300;
  23.  
  24.   // print the rotation angle
  25.   Serial.print("Angle: ");
  26.   Serial.print(angle);
  27.   Serial.println(" degrees");
  28.  
  29.   // delay before reading from the sensor again
  30.   delay(1000);
  31. }
  32.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement