Ingo Lohs
Published © GPL3+

Grove - Introduction in Rotary Angle Sensor / Potentiometer

This project shows how to light a LED using a potentiometer.

BeginnerProtip1 hour4,446
Grove - Introduction in Rotary Angle Sensor / Potentiometer

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Seeed Studio Grove Base Shield
×1
Seeed Studio Rotary Angle Sensor
or this one:
×1
Seeed Studio Sliding Potentiometer
×1
LED (generic)
LED (generic)
×1
Seeed Studio Grove - Universal 4 PIN Jumper
×1

Software apps and online services

Arduino Web Editor
Arduino Web Editor

Story

Read more

Code

Potentiometer-Example

C/C++
Original by Seeedstudio
/******************************************************************************/
    /*macro definitions of Rotary angle sensor and LED pin*/
    #define ROTARY_ANGLE_SENSOR A0
    #define LED 2//the Grove - LED is connected to D2 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(9600);
        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;
    }

Credits

Ingo Lohs

Ingo Lohs

182 projects • 194 followers
I am well over 50 years and come from the middle of Germany.

Comments