Arduino Special Inputs and Outputs
In addition to basic digital and analog I/O, the Arduino API provides several functions for generating a tone, measuring pulse duration, and sending or receiving individual bits under software-controlled clocking.
Difficulty: Intermediate
Technically reviewed: August 2026
Reference platform: General Arduino API; Arduino Uno R3 for the circuit example
Overview
tone()andnoTone(): generate and stop a square wave.pulseIn()andpulseInLong(): measure the duration of a HIGH or LOW pulse.shiftIn(): read one byte serially using a software-controlled clock.shiftOut(): send one byte serially using a software-controlled clock.
Availability, timing accuracy and hardware-resource use can depend on the Arduino core and board. For time-critical or high-speed applications, check the documentation for the selected platform.
Using tone()
tone(pin, frequency) generates a square wave with an approximately 50% duty cycle on the selected pin. Specify the frequency in hertz.
tone(pin, frequency);
tone(pin, frequency, duration);
The optional duration parameter specifies the signal duration in milliseconds. The call does not wait for the tone to finish. Without a duration, the signal remains active until the program calls noTone().
The standard Arduino implementation handles one tone at a time. Because timers are used internally, tone() can affect certain PWM outputs on some boards; this behaviour is platform-dependent.
A simple melody
const byte buzzerPin = 8;
const unsigned int melody[] = {262, 294, 330, 349, 392};
const byte noteDivisors[] = {4, 4, 4, 4, 2};
const byte noteCount = sizeof(melody) / sizeof(melody[0]);
void setup()
{
for (byte i = 0; i < noteCount; i++)
{
unsigned long noteDuration = 1000UL / noteDivisors[i];
tone(buzzerPin, melody[i], noteDuration);
delay(noteDuration * 13UL / 10UL);
noTone(buzzerPin);
}
}
void loop() {}
Using noTone()
noTone(pin) stops the tone previously started on the specified pin.
noTone(buzzerPin);
Measuring pulse duration with pulseIn()
pulseIn() waits for the input to enter the requested state, measures how long it remains in that state, and returns the pulse duration in microseconds. The measured state can be HIGH or LOW.
unsigned long duration = pulseIn(pin, HIGH, timeout);
timeout sets the maximum wait in microseconds. If no complete pulse is detected before the timeout, the function returns 0. When omitted, the default timeout is one second.
const byte pulsePin = 6;
void setup()
{
Serial.begin(115200);
pinMode(pulsePin, INPUT);
}
void loop()
{
unsigned long duration = pulseIn(pulsePin, HIGH, 100000UL);
if (duration == 0)
Serial.println("No complete pulse received");
else
Serial.println(duration);
}
pulseIn() is blocking, so the program cannot perform other work during the measurement. Always choose a timeout appropriate for the expected signal. Unlike the old example, do not disable interrupts unnecessarily around the measurement.
Measuring longer pulses with pulseInLong()
pulseInLong() provides a similar interface but is designed for measuring longer pulses and requires interrupts to remain enabled. Support and measurement limits can depend on the Arduino core.
unsigned long duration = pulseInLong(pulsePin, HIGH, 3000000UL);
Reading a byte with shiftIn()
shiftIn() reads eight bits from a data pin while the program controls the clock pin. Bit order can be MSBFIRST or LSBFIRST.
const byte dataPin = 2;
const byte clockPin = 3;
void setup()
{
Serial.begin(115200);
pinMode(dataPin, INPUT);
pinMode(clockPin, OUTPUT);
digitalWrite(clockPin, LOW);
}
void loop()
{
byte value = shiftIn(dataPin, clockPin, MSBFIRST);
Serial.println(value, BIN);
delay(500);
}
shiftIn() is not a complete SPI implementation: it does not automatically manage chip select, transactions or SPI modes. For a true SPI peripheral, the Arduino SPI library is usually the appropriate choice.
Sending a byte with shiftOut()
shiftOut() sends the eight bits of one byte through the data pin using a clock generated by the program.
const byte dataPin = 2;
const byte clockPin = 3;
void setup()
{
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
digitalWrite(clockPin, LOW);
byte value = 0b10010111;
shiftOut(dataPin, clockPin, MSBFIRST, value);
}
void loop() {}
The fourth parameter is one byte. Send larger values with multiple calls, one byte at a time and in the byte order expected by the receiving device.
uint16_t value = 500;
shiftOut(dataPin, clockPin, MSBFIRST, highByte(value));
shiftOut(dataPin, clockPin, MSBFIRST, lowByte(value));
Controlling a 74HC595 shift register
The 74HC595 is a serial-in, parallel-out shift register. It allows eight digital outputs to be controlled with three Arduino pins: data, clock and storage-register clock, commonly called the latch signal.

const byte latchPin = 8;
const byte clockPin = 12;
const byte dataPin = 11;
void setup()
{
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
digitalWrite(latchPin, HIGH);
digitalWrite(clockPin, LOW);
}
void loop()
{
for (int value = 0; value < 256; value++)
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, (byte)value);
digitalWrite(latchPin, HIGH);
delay(100);
}
}
How the Arduino program works
- While the latch signal is low,
shiftOut()loads eight bits into the shift register. - When the latch signal goes high, the new value appears on all outputs at the same time.
- The loop sends every bit pattern from 0 to 255.
- Each LED requires its own current-limiting resistor, and the total output current must remain within the limits specified in the IC datasheet.
🧪 Try it!
- Change the notes and durations in the melody example.
- Use a short timeout with
pulseIn()and observe the 0 return value when no signal is present. - Switch between
MSBFIRSTandLSBFIRSTand observe the LED order. - Modify the 74HC595 example to display a running light with the
bit()macro.
Common mistakes
- No timeout: pulse measurement can block the program for a long time.
- Interrupts disabled: this can reduce timing accuracy and prevent
pulseInLong()from working. - Wrong bit or byte order: the peripheral datasheet defines the required order.
- Confusing shiftIn or shiftOut with SPI: these are simple software-clocked byte operations.
- No common ground: connect the grounds of the Arduino and peripheral.
- Overloaded outputs: check the current limits of both the peripheral and microcontroller.
Summary
tone() generates a square wave, pulseIn() and pulseInLong() measure pulse duration, and shiftIn() and shiftOut() transfer one byte bit by bit. These are convenient Arduino APIs, but their blocking behaviour, accuracy and platform-dependent limitations must be considered.
