ReadAnalogInput


In this new example, you are going to read analog input. You'll use a potentiometer.
The potentiometer can provide a certain range of resistance. When you twist the knob, the resistance will change, thus the input voltage will change with it.
When SwiftIO board reads from the pin, it will get a number between 0 and 4095. And then change it into a digital number between 0 and 3.3V.
What you need
SwiftIO board
Potentiometer
Jumper wires
Circuit

Let's build the circuit now.
The potentiometer has three legs:
the first leg on the left goes to analog pin A6
the second leg goes to power
the third leg goes to ground
Different potentiometer may vary, so please refer to its manual before building the circuit.
Code
You can find the example code at the bottom left corner of IDE:
> GettingStarted > ReadAnalogInput. Well, you may try to read from to other modules, like sensors. It's quite interesting.
For the code, you will use the AnalogIn class.
Instruction
The parameter passed by the object AnalogIn must be the port (A0-A11) that can be used for analog input in the enumeration Id.
There are three methods for returning the analog value in this object. .readVoltage() returns the voltage value . The return value is a floating point number between 0V-3.3V.
print() function is to print the result directly to the serial port. You can conveniently use a computer to connect to the serial port of the SwiftIO Board to view the results and debug.
Please note that the SwiftIO Board has two USB ports. The port used to load programs cannot be used as a monitor serial port, so you need to change the line. For details, please see the Tips operations below. Both USB ports can be used as power supply ports for SwiftIO Board.
See Also
Id - Enumerations of all pins on the board
AnalogIn.readRawValue() - Read the current raw value from the specified analog pin.
References
Tips

Last updated
Was this helpful?