Analog I/O

analogRead(pin)

  • pin <number> The pin number which can support ADC function.

  • Returns: <number>, the value is in between 0.0 ~ 1.0

Read and return analog signal value from the ADC pin. A RangeError will be thrown if the pin does not support ADC function.

// Read analog value on pin 26.
var pin = 26;
var value = analogRead(pin);

analogWrite(pin[, duty[, frequency]])

  • pin <number> The pin number which can support PWM function.

  • duty <number> The PWM duty cycle between 0.0 and 1.0. Default: 0.5

  • frequency <number> The PWM frequency in Hz. Default: 490Hz

Generate PWM signal with specific frequency and duty to the PWM pin. A RangeError will be thrown if pin does not support PWM function.

// Generate 490 Hz 50% duth PWM signal.
var pin = 0;
analogWrite(pin, 0.5);

tone(pin[, frequency[, duration[, duty]]])

  • pin <number> The pin number which can support PWM function.

  • frequency <number> The PWM frequency in Hz. Default: 261.626Hz (C Key)

  • duration <number> The duration in milliseconds if a duration is not 0, duration 0 is forever. Default: 0

  • duty <number> The PWM duty cycle between 0.0 and 1.0. Default: 0.5

Generate sound tone(PWM) on the PWM pin for duration. A frequency and duty can be set. A RangeError will be thrown if pin does not support PWM function.

// Generate 300 Hz tone on the pin 0 for 1sec.
tone(0, 300, 1000); // Generate 300Hz tone on the pin 0 for 1sec.

noTone(pin)

  • pin <number> The pin number which can support PWM function.

Stop the tone one the PWM pin. A RangeError will be thrown if pin does not support PWM function.

// Generate 200 Hz tone to the pin 0 and stop the tone after 1sec.
tone(0, 200); // Generate 200Hz tone on the pin 0.
delay(1000); // Wait for 1000ms (1sec)
noTone(0); // Stop the tone on the pin 0