Example Code for Arduino-Angle Detection

Last revision 2025/12/29

This tutorial demonstrates how to use hall angle sensor in 5 minutes. The sensors detect turn round and users can learn how to read the analog signal from the sensor to calculate the rotation angle.

Hardware Preparation

Software Preparation

Wiring Diagram

SEN0221 Gravity: 360 Degree Hall Angle Sensor Connection Diagram

Other Preparation Work

Sample Code

/*!
 * @file  hallAngleSensor.ino
 * @brief  This example The sensors detect turn round
 * @copyright  Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
 * @license  The MIT License (MIT)
 * @author  [email protected]
 * @version  V1.0
 * @date  2016-7-12
 */

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  float analogValue = analogRead(A2);     //Voltage reading
  float amp = analogValue / 1024.0 * 360; //Angle calculation  (  UNO is a 10-bit AD )
  Serial.print("Angle:");
  Serial.println(amp);
  delay(500);
}

Result

The serial monitor shows the angle value.

Was this article helpful?

TOP