Loading...
Searching...
No Matches
Analog I/O

Reading A0-A5 with analogRead() (0-1023) and PWM with analogWrite() (0-255), with their settings. More...

Functions

 analogReadResolution (bits)
 Sets the number of bits analogRead() returns.
 analogRead (pin)
 Reads an analogue pin.
 analogWriteResolution (bits)
 Sets the range analogWrite() takes.
 analogWriteFreq (hz)
 Sets the PWM frequency for analogWrite() on pins set up after this.
 analogWrite (pin, value)
 Puts a PWM signal on a pin: a fraction of the time high.

Variables

str A0 = "A0"
 Analogue pin A0 (RP2040 ADC, 0-3.3 V).
str A1 = "A1"
 Analogue pin A1 (RP2040 ADC, 0-3.3 V).
str A2 = "A2"
 Analogue pin A2 (RP2040 ADC, 0-3.3 V).
str A3 = "A3"
 Analogue pin A3 (MAX11635, 0-2.048 V).
str A4 = "A4"
 Analogue pin A4 (MAX11635, 0-2.048 V).
str A5 = "A5"
 Analogue pin A5 (MAX11635, 0-2.048 V).

Detailed Description

Reading A0-A5 with analogRead() (0-1023) and PWM with analogWrite() (0-255), with their settings.

Function Documentation

◆ analogReadResolution()

analogReadResolution ( bits)

Sets the number of bits analogRead() returns.

Below 12 bits, analogRead() drops the lowest bits of the reading; above 12 it adds zero bits, which widens the range but adds no detail.

Parameters
bits1-16. 10 (0-1023) at start-up; 12 (0-4095) is the ADCs' own resolution.
Exceptions
ValueErrorbits is outside 1-16.

◆ analogRead()

analogRead ( pin)

Reads an analogue pin.

At 10 bits each step is 3.2 mV on A0-A2 and 2 mV on A3-A5, so the two groups give different numbers for the same voltage. Keep A3-A5 at or below 2.048 V. Averaging is set with analog_averaging() in phyduino.py.

Parameters
pinA0-A5, or 0-5 for A0-A5.
Returns
0-1023 at the default 10 bits. A0-A2: top = 3.3 V. A3-A5: top = 2.048 V.
Exceptions
ValueErrorNot an analogue pin.
OSErrorThe external ADC did not respond (A3-A5).
Example
millivolts = analogRead(A0) * 3300 / 1024 # A0's reading as a voltage

◆ analogWriteResolution()

analogWriteResolution ( bits)

Sets the range analogWrite() takes.

For example 10 takes 0-1023, and 16 takes 0-65535.

Parameters
bits1-16. 8 (0-255) at start-up.
Exceptions
ValueErrorbits is outside 1-16.

◆ analogWriteFreq()

analogWriteFreq ( hz)

Sets the PWM frequency for analogWrite() on pins set up after this.

Pins already on PWM keep their frequency. Pins next to each other on a PWM slice share one frequency (see pwm() in phyduino.py), so setting up one can change its neighbour's.

Parameters
hzFrequency in Hz, 8 Hz and up. 1000 at start-up.

◆ analogWrite()

analogWrite ( pin,
value )

Puts a PWM signal on a pin: a fraction of the time high.

Pairs of pins share a PWM output; see pwm() in phyduino.py. The signal runs at analogWriteFreq() (1000 Hz at start-up) and keeps going while your code runs, until another analogWrite(), digitalWrite() or pinMode() on the pin. A meter on the pin reads the average: 128 gives about 1.65 V.

Parameters
pinPin number (3 is D3) or name ("D3", "A0", LED_BUILTIN).
value0 (always low) to 255 (always high) at the default 8 bits; values outside the range are clamped.
Exceptions
ValueErrorUnknown pin, an analogue-only pin (A3-A5), or the pin shares its PWM output with one already in use.
Example
analogWrite(LED_BUILTIN, 64) # the LED at about a quarter brightness

Variable Documentation

◆ A3

str A3 = "A3"

Analogue pin A3 (MAX11635, 0-2.048 V).

Analogue only.

◆ A4

str A4 = "A4"

Analogue pin A4 (MAX11635, 0-2.048 V).

Analogue only.

◆ A5

str A5 = "A5"

Analogue pin A5 (MAX11635, 0-2.048 V).

Analogue only.