Grove - Rotary Angle Sensor
The rotary angle sensor produces analog output between 0 and Vcc (5V DC with Seeeduino) on its D1 connector.
The D2 connector is not used. The angular range is 300 degrees with a linear change in value.
The resistance value is 10k ohms, perfect for Arduino use. This may also be known as a “potentiometer ”.
Hardware Required
- Intel® Edison for Arduino
- Grove Base Shield v2
- Grove Button(P)
- Grove LED Socket Kit
- Working Voltage - 5V
Circuit
Schematic
Code
/******************************************************************************/
int potentiometer = 0;
void setup()
{
Serial.begin(9600); //set the serial communication frequency at 9600 bits per sec
pinMode(potentiometer, INPUT);
}
void loop()
{
int value = analogRead(potentiometer);
Serial.println(value); //pirnt the value on the serial monitor screen
delay(1000); //wait 1000ms before printing next value
}
/******************************************************************************/
/*macro definitions of Rotary angle sensor and LED pin*/
#define ROTARY_ANGLE_SENSOR 0
#define LED 3 //the Grove - LED is connected to D3 of Arduino
#define ADC_REF 5 //reference voltage of ADC is 5v.If the Vcc switch on the seeeduino
//board switches to 3V3, the ADC_REF should be 3.3
#define GROVE_VCC 5 //VCC of the grove interface is normally 5v
#define FULL_ANGLE 300 //full value of the rotary angle is 300 degrees
void setup()
{
Serial.begin(115200);
pinsInit();
}
void loop()
{
int degrees;
degrees = getDegree();
Serial.println("The angle between the mark and the starting position:");
Serial.println(degrees);
int brightness;
/*The degrees is 0~300, should be converted to be 0~255 to control the*/
/*brightness of LED */
brightness = map(degrees, 0, FULL_ANGLE, 0, 255);
controlBrightness(brightness);
delay(500);
}
void pinsInit()
{
pinMode(ROTARY_ANGLE_SENSOR, INPUT);
pinMode(LED,OUTPUT);
}
/*PWM control brightness */
/*If brightness is 0,the LED is off. */
/*The Greater the brightness, the brighter the LED.*/
/*The range of brightness is 0~255 */
void controlBrightness(int brightness)
{
analogWrite(LED,brightness);
}
/************************************************************************/
/*Function: Get the angle between the mark and the starting position */
/*Parameter:-void */
/*Return: -int,the range of degrees is 0~300 */
int getDegree()
{
int sensor_value = analogRead(ROTARY_ANGLE_SENSOR);
float voltage;
voltage = (float)sensor_value*ADC_REF/1023;
float degrees = (voltage*FULL_ANGLE)/GROVE_VCC;
return degrees;
}
See
Also
Reference sites
'IoT > Intel Edison' 카테고리의 다른 글
Grove – Servo (0) | 2015.02.14 |
---|---|
Grove - LCD RGB Backlight (0) | 2015.02.09 |
Grove - Sound Sensor (0) | 2015.01.24 |
Grove - Buzzer (0) | 2015.01.24 |
Grove - Button and LED (0) | 2015.01.22 |