Analog Joystick

Connect an analog joystick to an Arduino. Read two analog values (x- and y-position) and a switch using a method witch does not hog the processor during the whole ADConversion.

The capacitor reduces digital noise on the analog readings.
Joystick

Code

static const byte ADC_CHANNEL_JOYSTICK_X = 0;
static const byte ADC_CHANNEL_JOYSTICK_Y = 1;

static const byte ADC_CHANNEL_MAX = 2;

byte adc_channel = 0;
bool joystick_button = 0; // 1 or 0
uint16_t joystick_x = 0; // 0~1023
uint16_t joystick_y = 0; // 0~1023


void setup() {
 Serial.begin(115200); // Remove
 
 pinMode(A0, INPUT); // Analog pin A0
 pinMode(A1, INPUT); // Analog pin A1
 pinMode(2, INPUT_PULLUP); // Digital pin 2
 
 // Setup adc
 ADCSRA |= 1 << ADEN; // Enable ADC, with no interrupt!
 ADCSRA |= ((1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0)); // Main clock / 128
 ADMUX = adc_channel; // Channel
 ADMUX |= 1 << REFS0; // Aref pin -> 100nF -> gnd
 ADCSRA |= 1 << ADSC; // Start first conversion
}


void loop() {
 byte changed = 0;
 
 // Analog
 if ((ADCSRA & (1 << ADSC)) == 0) { // ADC done
 changed += read_analog();
 }

 // Digital
 changed += read_digital();

 // Tmp
 if(changed > 0) {
 Serial.print("x: ");
 Serial.println(joystick_x);
 Serial.print("y: ");
 Serial.println(joystick_y);
 Serial.print("button: ");
 Serial.println(joystick_button);
 Serial.println();
 }
 delay(100); // Remove
}


byte read_digital() {
 byte state = 0;
 byte val = !(bool)(PIND & 0b00000100); // PORT IN D, pin nr 2
 if(val != joystick_button) {
 state = 1;
 joystick_button = val;
 }
 return state;
}


byte read_analog()
{
 byte state = 0;
 uint16_t val = ADC; // ADC is the full 10-bit, two-register, value from the last ADConversion
 
 switch(adc_channel) {
 case ADC_CHANNEL_JOYSTICK_X:
 if(joystick_x != val) {
 state = 1;
 joystick_x = val;
 }
 break;
 case ADC_CHANNEL_JOYSTICK_Y:
 if(joystick_y != val) {
 state = 1;
 joystick_y = val;
 }
 break;
 }

 adc_channel++;
 if(adc_channel >= ADC_CHANNEL_MAX) adc_channel = 0;
 ADMUX = adc_channel; // Channel
 ADMUX |= 1 << REFS0; // Aref pin -> 100nF -> gnd
 ADCSRA |= 1 << ADSC; // Start next conversion
 
 return state;
}


Light sensor, LDR

The light sensor is also called a light-dependent resistor (LDR) or photocell. It is connected to Arduino pin A0. analogRead measures the voltage at the pin. You will get a value between 0 and 1023 (where 0 equals ground and 1023 equals +5 volts).

Changing the type of lamps and ambient light will give a bit of variation in your readings. In many cases a covered sensor equals about 500 and an exposed sensor is around 800. So under those circumstances an ON / OFF threshold-value of about 700 would be fine.

 

 

Light sensor


 

 

Code

void setup() {
  Serial.begin(9600); // Set up serial communication with the computer
}
 
void loop() {
  int lightSensorValue = analogRead(A0);     // Read the input on analog pin 0
  if(lightSensorValue > 700) {
      Serial.println("Light switch is ON");  // Sensor is receiving light
  } else {
      Serial.println("Light switch is OFF"); // Sensor is covered
  }
  delay(1);                          // Don't do analogRead too often
}

Read a pot, control 5 LEDs

The pot controls which LED is turned on. The pot is connected to Arduino pin A0. analogRead is used to measure the voltage at A0.

You will get a value between 0 and 1023 (where 0 equals ground and 1023 equals +5 volts).  The analog range is divided into 5 sections (1023 / 5).

Five LEDs are connected to the digital pins 2, 3, 4, 7 and 8. Use a switch statement to address individual pins depending on pot rotation.

Use a small delay to let the circuitry rest before doing the subsequent read, or the value can be erroneous.

Connect a Potentiometer

 

5 LEDs to individual pins

Code

void setup() {
  Serial.begin(9600); // Set up serial communication with the computer
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
}
 
void loop() {
  int analogValue = analogRead(A0);  // Read the input on analog pin 0
  int pinSelect = analogValue / 204;
  Serial.println(pinSelect);         // Print value to serial monitor
  delay(1);                          // Don't do analogRead too often
  
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(7, HIGH);
  digitalWrite(8, HIGH);
  
  switch(pinSelect) {
    case 0:
      digitalWrite(2, LOW);
      break;
    case 1:
      digitalWrite(3, LOW);
      break;
    case 2:
      digitalWrite(4, LOW);
      break;
    case 3:
      digitalWrite(7, LOW);
      break;
    case 4:
      digitalWrite(8, LOW);
      break;
    
  }
}

Read a pot, print to Serial Monitor

The USB connection is used for serial communication between Arduino and computer.

The potentiometer pin 2 is connected to Arduino pin A0. analogRead is then used to measure the voltage at the pin. (The potentiometer rating doesn’t matter, but avoid lower resistances than 5k, so you don’t get an uneconomically high current flow.) You will get a value between 0 and 1023 (where 0 equals ground and 1023 equals +5 volts).

Use a small delay to let the circuitry rest before doing the subsequent read, or the value can be erroneous.

Code

void setup() {
  Serial.begin(9600); // Set up serial communication with the computer
}
 
void loop() {
  int analogValue = analogRead(A0);  // Read the input on analog pin 0
  Serial.println(analogValue);       // Print value to serial monitor
  delay(1);                          // Don't do analogRead too often
}