Grove - Introduction in Rotary Angle Sensor / Potentiometer

Grove - Introduction in Rotary Angle Sensor / Potentiometer © GPL3+

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

  • 1,320 views
  • 0 comments
  • 2 respects

Components and supplies

Apps and online services

About this project

I would like to share my first steps with the Grove 'plug & play' components.

Overview

This is primarily about the Rotary Angle sensor or as an alternative the sliding potentiometer. Once the Grove Base Shield is attached to the Arduino Uno R3, the Grove universal cable can be used to connect the sensor to one of the analog ports - here A0.

Getting It to Work

I use the Arduino WebIDE and have the code of Seeedstudio available in the facility for copy & paste. A Libary is not necessary. The values ​​determined in the serial monitor of the WebIDE are listed accordingly. Potentiometers can be used as soon as an adjustment of the input is in the project.

Unfortunately I could not perceive the change by the potentiometer to the brightness of the generic LED (long leg to yellow, short leg to black / GND).

Conclusion

This short introduction reflects the available original example of Seeedstudio. Further examples with a introduction in components from Grove Starter Kit can be found here:

More projects under my account.

Code

Potentiometer-ExampleC/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;
    }

Comments

Similar projects you might like

Stepper - A First Introduction to Nema 17

by Ingo Lohs

  • 5,643 views
  • 6 comments
  • 43 respects
Add projectSign up / Login