So far, you've learned about digital signals. In this example, let's try analog signals. You'll turn a potentiometer to change the input. Your board reads its values and shows them on a serial monitor.

## What you need​

• SwiftIO Micro (or SwiftIO board)
• Potentiometer
• Jumper wires

## Circuit​

Let's build the circuit now.

1. Place the potentiometer onto the breadboard.
2. Connect the first leg on the left to pin GND.
3. Connect the second leg to pin A0.
4. Connect the third leg goes to pin 3V3. ## Example code​

For the code, you will use the `AnalogIn` class.

Open the project `ReadAnalogInput` in the folder `MadExamples`/`Examples`/`GettingStarted` if you downloaded the examples.

``// Read the input voltage on a specified analog pin. The value you get will be a decimal between 0.0 and 3.3.// Import the library to enable the relevant classes and functions.import SwiftIOimport MadBoard@mainpublic struct ReadAnalogInput {    public static func main() {        // Initialize the pin A0 as an analog input pin.        let pin = AnalogIn(Id.A0)        // Read the input voltage every second.        while true {            // Declare a constant to store the value you read from the analog pin.            let value = pin.readVoltage()            // Print the value and you can see it in the serial monitor.            print(value)            // Wait a second and then continue to read.            sleep(ms: 1000)        }    }}``

## Background​

You have known that the digital signal has determined values, and the analog signal is quite different. Its voltage changes smoothly with time. And its value ranges between 0V and 3.3V. So you can get 1.5V, 2V, 2.75V... There can be infinite possible values. So how does the board read the values? Here comes the analog to digital converter. In brief, it converts the analog voltage to a digital value that the microcontroller can read.

The converter has different precision, and the resolution describes the possible values it can measure. Our boards have a 12-bit resolution, which means there are 4096 (0-4095) values in total. The values from 0 to 4095 are known as raw values.

Let's see the working process in detail. When the board reads from the analog pin, it will first get a raw value between 0 and 4095, which will be mapped to the voltage value proportionally. Here is the formula:

resolution / reference voltage = raw value / actual voltage

where

resolution = 4096

reference voltage = 3.3V

For example, if the raw value equals 0, the voltage would be 0V; if the raw value equals 4095, the voltage would be 3.3V; and 2047 corresponds to 1.65V.

### Potentiometer​

The potentiometer is one kind of variable resistor. You could adjust its resistance by rotating it clockwise or anticlockwise. The resistance between ① and ③ is its maximum value. The wiper divides it into two parts. As the wiper moves, the resistance of the two parts will change accordingly.

## Code analysis​

``import SwiftIOimport MadBoard``

Import the SwiftIO library to use the related functionalities of your board and the MadBoard library to use the corresponding pin id.

``let pin = AnalogIn(Id.A0)``

The potentiometer connects to pin A0, so you initialize it before using the pin.

``let value = pin.readVoltage()``

The method `.readVoltage()` returns directly the voltage value. The return value is a floating-point number between 0V-3.3V.

``print(value)``

Print the result directly to the serial port. You can view the results on the serial monitor.

## Reference​

• `init(_:)` - initialize an analog input pin. You need to tell the id of a specified pin to initialize it.
• `readVoltage()` - read the input voltage from a pin. It will return a float between 0 and 3.3.
{"ft_lang_label":"__label__en","ft_lang_prob":0.77508056,"math_prob":0.9511604,"size":3685,"snap":"2023-40-2023-50","text_gpt3_token_len":894,"char_repetition_ratio":0.13583265,"word_repetition_ratio":0.00953895,"special_character_ratio":0.236635,"punctuation_ratio":0.12342216,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9907495,"pos_list":[0,1,2,3,4,5,6],"im_url_duplicate_count":[null,2,null,3,null,3,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-11-28T17:10:40Z\",\"WARC-Record-ID\":\"<urn:uuid:12fa7bbf-08ea-4663-9dae-afc0e3787d03>\",\"Content-Length\":\"47497\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:550dfb54-b1da-44cd-b532-2ea18fcb9a90>\",\"WARC-Concurrent-To\":\"<urn:uuid:d7974758-fe61-41f2-9bd0-0157b09fea39>\",\"WARC-IP-Address\":\"172.67.222.39\",\"WARC-Target-URI\":\"https://docs.madmachine.io/projects/general/getting-started/read-analog-input\",\"WARC-Payload-Digest\":\"sha1:BXZQ7MHVGAIQ7CEHCDWUNT5SWDGIIIPC\",\"WARC-Block-Digest\":\"sha1:ZYJI7P2OQ5SUCCAMMSJL2TACO2XG7MK6\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-50/CC-MAIN-2023-50_segments_1700679099892.46_warc_CC-MAIN-20231128151412-20231128181412-00506.warc.gz\"}"}