SODAQ
Connect Anything Anywhere

3. Using a Potentiometer with SODAQ to measure an angle
In this tutorial you will learn about how an analog (potentiometer) wind sensor could collect data, and to display information on the serial monitor of the Integrated Development Environment. It also uses a reference voltage to compare to the voltage that comes through the sensor.
Required components:
SODAQ Moja
0.5W Solar Panel
1aH battery pack
Grove Rotary Angle Sensor
Grove w/o Buckle Cable (any length)
First, plug the Rotary Angle Sensor into the A0/A1 pin in the always on row. Next, plug the 0.5W solar panel and the 1A LiPo battery into the respective sockets (see http://www.sodaq.net/#!getting-started/c21ma for a board diagram)
Turn on the SODAQ board, compile and upload the following sketch from the arduino IDE onto the SODAQ Board, and open the Serial Monitor in the IDE:

#define ROTARY_ANGLE_SENSOR A0
#define ADC_REF 3.3 //reference voltage of ADC is 3v3
#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); delay(500);
}
void pinsInit()
{
pinMode(ROTARY_ANGLE_SENSOR, INPUT);
}
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;
}