Skip to main content

Advanced I/O

Extension Input/Output (Tone Output and Shift Input)

This library is used to generate a square wave, input/output serial data (software), and pulse width measurement.

tone

Description
Generates a square wave for the specified frequency. Useful for turning on a buzzer.
Syntax
tone(int pin, int frequency, int duration)
Parameters
pin: The pin on which to generate the tone
frequency: The frequency of the tone in hertz [Hz]
duration: The duration of the tone in milliseconds (optional)
Continues to buzz when abbreviated or set to 0
Returns
Nothing
Note
Available pins are 0, 3 and 5.

noTone

Description
Stops the generation of a square wave triggered by tone(). Has no effect if no tone is being generated.
Syntax
noTone(int pin)
Parameters
pin: The pin on which to stop generating the tone.
Returns
Nothing

shiftOut

Description
Shifts out a byte of data one bit at a time. Starts from either the most (the leftmost) or least (the rightmost) significant bit. Each bit is written in turn to a data pin, after which a clock pin is pulsed (taken high, then low) to indicate that the bit is available.
Syntax
shiftOut(int dataPin, int clockPin, bitOrder, unsigned char value)
Parameters
dataPin: The pin on which to output each bit
clockPin: The pin to toggle once the dataPin has been set to the correct value
bitOrder: Which order to shift out the bits; either MSBFIRST or LSBFIRST (Most Significant Bit First, or, Least Significant Bit First)
value: The data to shift out. (unsigned char 8-bit data)
Returns
Nothing

shiftIn

Description
Shifts in a byte of data one bit at a time. Starts from either the most (the leftmost) or least (the rightmost) significant bit. For each bit, the clock pin is pulled high, the next bit is read from the data line, and then the clock pin is taken low.
Syntax
unsigned char shiftIn(unsigned char dataPin, unsigned char clockPin, bitOrder)
Parameters
dataPin: The pin on which to input each bit
clockPin: The pin to toggle to signal a read from the dataPin
bitOrder: Which order to shift in the bits; either MSBFIRST or LSBFIRST
Returns
The value read (8-bit data)

shiftOutEx

Description
Shifts out multiple bytes(up to 32 bits) of data one bit at a time. Each bit is written in turn to a data pin, after which a clock pin is pulsed (taken high, then low) to indicate that the bit is available.
Syntax
shiftOutEx(int dataPin, int clockPin, bitOrder, int len, unsigned long value)
Parameters
dataPin: The pin on which to output each bit
clockPin: The pin to toggle once the dataPin has been set to the correct value
bitOrder: Which order to shift out the bits; either MSBFIRST or LSBFIRST
len: Number of output bits
value: The data to shift out (32 bits or less data. In the case of MSBFIRST, the most significant bit (MSB) should be filled.)
Returns
Nothing

shiftInEx

Description
Shifts in multiple bytes (up to 32 bits) of data one bit at a time. Starts from either the most (the leftmost) or least (the rightmost) significant bit. For each bit, the clock pin is pulled high, the next bit is read from the data line, and then the clock pin is taken low.
Syntax
uint32_t shiftIn(unsigned char dataPin, unsigned char clockPin, bitOrder, int len)
Parameters
dataPin: The pin on which to input each bit
clockPin: The pin to toggle to signal a read from dataPin
bitOrder: Which order to shift in the bits; either MSBFIRST or LSBFIRST
len: Number of input bits
Returns
The value read (8-bit data)

pulseIn

Description
Reads a pulse (either HIGH or LOW) on a pin.
Syntax
unsigned long pulseIn(int pin, int val, unsigned long timeout = 1000000)
Parameters
pin: The number of the pin on which you want to read the pulse
val: Type of pulse to read: either HIGH or LOW
timeout (optional): The number of microseconds to wait for the pulse to start; default is one second
Returns
The length of the pulse (in microseconds) or 0 if no pulse started before the timeout (unsigned long)

Sample Program

Sample for tone.


#include <Arduino.h>
void setup(){
    tone(3, 261);
    delay(500);
    tone(3, 293);
    delay(500);
    tone(3, 329);
    delay(500);
    noTone(3);
    delay(500);
}
    
void loop(){
}