This post is going to be about another fairly basic concept – reading analog values – but this is the first post in the series where I’ll connect external physical components to the device. In this post, I use a potentiometer to control the size of a potential difference, and use the AZ3166 MXChip device to read and display that difference. ## Setting up the device and potentiometer

The physical set up of this experiment is straightforward, as shown in the photo above – I just connected a potentiometer to the external edge connector, with 3v and 0v connectors to each end of the potentiometer, and using physical pin 5 to measure the potential difference as the rotating element moves between the ends.

My potentiometer was 2K, but that was just what I had closest to hand.

## Writing the code

This code is very similar to the standard Arduino example to measure analog potential difference.

Physical pin 5 on the edge connector translates to Arduino virtual pin 4, so in my code the sensor pin is set to 4.

The analogRead function stores a representation of the potential difference in values from 0 to 1023, where 0 represents 0v and 1023 represents 3v.

I decided added a little bit of extra functionality to the sketch:

• The red LED’s brightness blinks with a delay in milliseconds corresponding to the analog value read – at 3v, the analog value is 1024 so the LED is red for 1.024s, and then switched off for 1.024s.
• I’ve also used the built in OLED display function to write the analog value on the device’s screen.

The code for the sketch is below:

```#include <OledDisplay.h>

int sensorPin = 4;
int RED_LED = 20;
int inputValue = 0;

void setup() {
Screen.init();
}

void loop() {

// Write heading to OLED display
Screen.print("Analog Value:", false);

// Read potential difference through sensor pin

// Write this value to the OLED screen
char buf;
sprintf(buf, "%d", inputValue);
Screen.print(1, buf);

// Now flash the red LED.
// The analog input is between 0 and 1024
// and therefore the delay will be between
// 0ms and 1024ms
digitalWrite(RED_LED, HIGH);
delay(inputValue);
digitalWrite(RED_LED, LOW);
delay(inputValue);
}
```

And the photo below shows the analog value written to the OLED display. ## Wrapping up

This was a very short post about reading analog values with the AZ3166, which is a well understood concept in the Arduino world. We can use the analogRead function with the AZ3166 in the same way as we use it in Arduino code, and can read analog signals through physical pin 5 (Arduino virtual pin 4). The example where we read the how a voltage is divided by a potentiometer isn’t hugely useful on its own, but it’s a useful concept to understand to the AZ3166 for other projects – we can read values many other external peripherals using the same principles.

## 3 thoughts on “Programming the AZ3166 MXChip Azure DevKit – reading analog values through physical pins”

1. JHOAN B. HENRICHE says:

2. lizerd71 says: