0% found this document useful (0 votes)
137 views25 pages

Ardunio Language Reference

The document provides information on several Arduino functions for digital and analog input/output: - digitalRead() reads the value (HIGH or LOW) from a digital pin. - digitalWrite() writes a HIGH or LOW value to a digital pin to set its voltage. - pinMode() configures a pin as an input, output, or input with pull-up resistor. - analogRead() reads the value (0-1023) from an analog pin based on the input voltage. - analogReference() configures the reference voltage used for analog readings. It describes the parameters, return values, example usage, and notes for each function.

Uploaded by

Alamco
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
137 views25 pages

Ardunio Language Reference

The document provides information on several Arduino functions for digital and analog input/output: - digitalRead() reads the value (HIGH or LOW) from a digital pin. - digitalWrite() writes a HIGH or LOW value to a digital pin to set its voltage. - pinMode() configures a pin as an input, output, or input with pull-up resistor. - analogRead() reads the value (0-1023) from an analog pin based on the input voltage. - analogReference() configures the reference voltage used for analog readings. It describes the parameters, return values, example usage, and notes for each function.

Uploaded by

Alamco
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

digitalRead()

[Digital I/O]

Description

Reads the value from a specified digital pin, either   HIGH  or  LOW .

Syntax

digitalRead(pin)

Parameters

pin : the Arduino pin number you want to read

Returns

HIGH  or  LOW

Example Code

Sets pin 13 to the same value as pin 7, declared as an input.

int ledPin = 13; // LED connected to digital pin 13


int inPin = 7; // pushbutton connected to digital pin 7
int val = 0; // variable to store the read value

void setup() {
pinMode(ledPin, OUTPUT); // sets the digital pin 13 as output
pinMode(inPin, INPUT); // sets the digital pin 7 as input
}

void loop() {
val = digitalRead(inPin); // read the input pin
digitalWrite(ledPin, val); // sets the LED to the button's value
}

Notes and Warnings


If the pin isn’t connected to anything,  digitalRead()  can return
either  HIGH  or  LOW  (and this can change randomly).

The analog input pins can be used as digital pins, referred to as A0,
A1, etc. The exception is the Arduino Nano, Pro Mini, and Mini’s A6
and A7 pins, which can only be used as analog inputs.

digitalWrite()
[Digital I/O]

Description

Write a  HIGH  or a  LOW  value to a digital pin.

If the pin has been configured as an  OUTPUT  with  pinMode() , its
voltage will be set to the corresponding value: 5V (or 3.3V on 3.3V
boards) for  HIGH , 0V (ground) for  LOW .

If the pin is configured as an  INPUT ,  digitalWrite()  will enable ( HIGH ) or


disable ( LOW ) the internal pullup on the input pin. It is recommended
to set the  pinMode()  to  INPUT_PULLUP  to enable the internal pull-up
resistor. See the Digital Pins  tutorial for more information.

If you do not set the  pinMode()  to  OUTPUT , and connect an LED to a
pin, when calling  digitalWrite(HIGH) , the LED may appear dim. Without
explicitly setting  pinMode() ,  digitalWrite()  will have enabled the internal
pull-up resistor, which acts like a large current-limiting resistor.

Syntax

digitalWrite(pin, value)

Parameters

pin : the Arduino pin number.


value :  HIGH  or  LOW .
Returns

Nothing

Example Code

The code makes the digital pin 13 an  OUTPUT  and toggles it by
alternating between  HIGH  and  LOW  at one second pace.

void setup() {
pinMode(13, OUTPUT); // sets the digital pin 13 as output
}

void loop() {
digitalWrite(13, HIGH); // sets the digital pin 13 on
delay(1000); // waits for a second
digitalWrite(13, LOW); // sets the digital pin 13 off
delay(1000); // waits for a second
}

Notes and Warnings

The analog input pins can be used as digital pins, referred to as A0,
A1, etc. The exception is the Arduino Nano, Pro Mini, and Mini’s A6
and A7 pins, which can only be used as analog inputs.

pinMode()
[Digital I/O]

Description

Configures the specified pin to behave either as an input or an


output. See the Digital Pins  page for details on the functionality of
the pins.

As of Arduino 1.0.1, it is possible to enable the internal pullup


resistors with the mode  INPUT_PULLUP . Additionally, the  INPUT  mode
explicitly disables the internal pullups.
Syntax

pinMode(pin, mode)

Parameters

pin : the Arduino pin number to set the mode of.


mode :  INPUT ,  OUTPUT , or  INPUT_PULLUP . See the Digital Pins  page for a
more complete description of the functionality.

Returns

Nothing

Example Code

The code makes the digital pin 13  OUTPUT  and Toggles
it  HIGH  and  LOW

void setup() {
pinMode(13, OUTPUT); // sets the digital pin 13 as output
}

void loop() {
digitalWrite(13, HIGH); // sets the digital pin 13 on
delay(1000); // waits for a second
digitalWrite(13, LOW); // sets the digital pin 13 off
delay(1000); // waits for a second
}

Notes and Warnings

The analog input pins can be used as digital pins, referred to as A0,
A1, etc.

analogRead()
[Analog I/O]

Description
Reads the value from the specified analog pin. Arduino boards
contain a multichannel, 10-bit analog to digital converter. This means
that it will map input voltages between 0 and the operating
voltage(5V or 3.3V) into integer values between 0 and 1023. On an
Arduino UNO, for example, this yields a resolution between readings
of: 5 volts / 1024 units or, 0.0049 volts (4.9 mV) per unit. See the
table below for the usable pins, operating voltage and maximum
resolution for some Arduino boards.

The input range can be changed using analogReference() , while the


resolution can be changed (only for Zero, Due and MKR boards)
using analogReadResolution() .

On ATmega based boards (UNO, Nano, Mini, Mega), it takes about


100 microseconds (0.0001 s) to read an analog input, so the
maximum reading rate is about 10,000 times a second.

OPERATING USABLE MAX


BOARD
VOLTAGE PINS RESOLUTION
A0 to
Uno 5 Volts 10 bits
A5
A0 to
Mini, Nano 5 Volts 10 bits
A7
Mega,
A0 to
Mega2560, 5 Volts 10 bits
A14
MegaADK
A0 to
Micro 5 Volts 10 bits
A11*
A0 to
Leonardo 5 Volts 10 bits
A11*
A0 to
Zero 3.3 Volts 12 bits**
A5
A0 to
Due 3.3 Volts 12 bits**
A11
MKR Family A0 to
3.3 Volts 12 bits**
boards A6

*A0 through A5 are labelled on the board, A6 through A11 are


respectively available on pins 4, 6, 8, 9, 10, and 12
**The default  analogRead()  resolution for these boards is 10 bits, for
compatibility. You need to use analogReadResolution()  to change it to
12 bits.

Syntax

analogRead(pin)

Parameters

pin : the name of the analog input pin to read from (A0 to A5 on most
boards, A0 to A6 on MKR boards, A0 to A7 on the Mini and Nano, A0
to A15 on the Mega).

Returns

The analog reading on the pin. Although it is limited to the resolution


of the analog to digital converter (0-1023 for 10 bits or 0-4095 for 12
bits). Data type:  int .

Example Code

The code reads the voltage on analogPin and displays it.

int analogPin = A3; // potentiometer wiper (middle terminal) connected to analog


pin 3
// outside leads to ground and +5V
int val = 0; // variable to store the value read

void setup() {
Serial.begin(9600); // setup serial
}

void loop() {
val = analogRead(analogPin); // read the input pin
Serial.println(val); // debug value
}

Notes and Warnings

If the analog input pin is not connected to anything, the value


returned by  analogRead()  will fluctuate based on a number of factors
(e.g. the values of the other analog inputs, how close your hand is to
the board, etc.).

analogReference()
[Analog I/O]

Description

Configures the reference voltage used for analog input (i.e. the value
used as the top of the input range). The options are:

Arduino AVR Boards (Uno, Mega, Leonardo, etc.)

 DEFAULT: the default analog reference of 5 volts (on 5V


Arduino boards) or 3.3 volts (on 3.3V Arduino boards)
 INTERNAL: an built-in reference, equal to 1.1 volts on the
ATmega168 or ATmega328P and 2.56 volts on the ATmega32U4 and
ATmega8 (not available on the Arduino Mega)
 INTERNAL1V1: a built-in 1.1V reference (Arduino Mega only)
 INTERNAL2V56: a built-in 2.56V reference (Arduino Mega only)
 EXTERNAL: the voltage applied to the AREF pin (0 to 5V only) is
used as the reference.

Arduino SAMD Boards (Zero, etc.)

 AR_DEFAULT: the default analog reference of 3.3V


 AR_INTERNAL: a built-in 2.23V reference
 AR_INTERNAL1V0: a built-in 1.0V reference
 AR_INTERNAL1V65: a built-in 1.65V reference
 AR_INTERNAL2V23: a built-in 2.23V reference
 AR_EXTERNAL: the voltage applied to the AREF pin is used as
the reference

Arduino megaAVR Boards (Uno WiFi Rev2)


 DEFAULT: a built-in 0.55V reference
 INTERNAL: a built-in 0.55V reference
 VDD: Vdd of the ATmega4809. 5V on the Uno WiFi Rev2
 INTERNAL0V55: a built-in 0.55V reference
 INTERNAL1V1: a built-in 1.1V reference
 INTERNAL1V5: a built-in 1.5V reference
 INTERNAL2V5: a built-in 2.5V reference
 INTERNAL4V3: a built-in 4.3V reference
 EXTERNAL: the voltage applied to the AREF pin (0 to 5V only) is
used as the reference

Arduino SAM Boards (Due)

 AR_DEFAULT: the default analog reference of 3.3V. This is the


only supported option for the Due.

Syntax

analogReference(type)

Parameters

type : which type of reference to use (see list of options in the


description).

Returns

Nothing

Notes and Warnings

After changing the analog reference, the first few readings


from  analogRead()  may not be accurate.

Don’t use anything less than 0V or more than 5V for external


reference voltage on the AREF pin! If you’re using an external
reference on the AREF pin, you must set the analog reference
to EXTERNAL before calling  analogRead() . Otherwise, you will
short together the active reference voltage (internally generated) and
the AREF pin, possibly damaging the microcontroller on your Arduino
board.

Alternatively, you can connect the external reference voltage to the


AREF pin through a 5K resistor, allowing you to switch between
external and internal reference voltages. Note that the resistor will
alter the voltage that gets used as the reference because there is an
internal 32K resistor on the AREF pin. The two act as a voltage
divider, so, for example, 2.5V applied through the resistor will yield
2.5 * 32 / (32 + 5) = ~2.2V at the AREF pin.

analogWrite()
[Analog I/O]

Description

Writes an analog value (PWM wave ) to a pin. Can be used to light a


LED at varying brightnesses or drive a motor at various speeds. After
a call to  analogWrite() , the pin will generate a steady rectangular wave
of the specified duty cycle until the next call to  analogWrite()  (or a call
to  digitalRead()  or  digitalWrite() ) on the same pin.

BOARD PWM PINS PWM FREQUENCY

Uno, Nano, 3, 5, 6, 9, 490 Hz (pins 5 and 6:


Mini 10, 11 980 Hz)
2 - 13, 44 - 490 Hz (pins 4 and
Mega
46 13: 980 Hz)
Leonardo, 3, 5, 6, 9, 490 Hz (pins 3 and
Micro, Yún 10, 11, 13 11: 980 Hz)
Uno WiFi
3, 5, 6, 9, 10 976 Hz
Rev.2
MKR boards * 0 - 8, 10, A3 732 Hz
(18), A4
BOARD PWM PINS PWM FREQUENCY

(19)
0 - 8, 10,
MKR1000
11, A3 (18), 732 Hz
WiFi *
A4 (19)
3 - 13, A0
Zero * (14), A1 732 Hz
(15)

Due ** 2-13 1000 Hz

pins 3 and 9: 490 Hz,


101 3, 5, 6, 9
pins 5 and 6: 980 Hz

* In addition to PWM capabilities on the pins noted above, the MKR


and Zero boards have true analog output when using  analogWrite()  on
the  DAC0  (A0 ) pin.
** In addition to PWM capabilities on the pins noted above, the Due
has true analog output when using  analogWrite()  on
pins  DAC0  and  DAC1 .

You do not need to call  pinMode()  to set the pin as an output before
calling  analogWrite() . The  analogWrite  function has nothing to do with
the analog pins or the  analogRead  function.

Syntax

analogWrite(pin, value)

Parameters

pin : the Arduino pin to write to. Allowed data types:  int .
value : the duty cycle: between 0 (always off) and 255 (always on).
Allowed data types:  int .

Returns

Nothing
Example Code

Sets the output to the LED proportional to the value read from the
potentiometer.

int ledPin = 9; // LED connected to digital pin 9


int analogPin = 3; // potentiometer connected to analog pin 3
int val = 0; // variable to store the read value

void setup() {
pinMode(ledPin, OUTPUT); // sets the pin as output
}

void loop() {
val = analogRead(analogPin); // read the input pin
analogWrite(ledPin, val / 4); // analogRead values go from 0 to 1023, analogWrite
values from 0 to 255
}

Notes and Warnings

The PWM outputs generated on pins 5 and 6 will have higher-than-


expected duty cycles. This is because of interactions with
the  millis()  and  delay()  functions, which share the same internal timer
used to generate those PWM outputs. This will be noticed mostly on
low duty-cycle settings (e.g. 0 - 10) and may result in a value of 0
not fully turning off the output on pins 5 and 6.

analogReadResolution()
[Zero, Due & MKR Family]

Description

analogReadResolution() is an extension of the Analog API for the


Arduino Due, Zero and MKR Family.

Sets the size (in bits) of the value returned by  analogRead() . It
defaults to 10 bits (returns values between 0-1023) for backward
compatibility with AVR based boards.
The Due, Zero and MKR Family boards have 12-bit ADC capabilities
that can be accessed by changing the resolution to 12. This will
return values from  analogRead()  between 0 and 4095.

Syntax

analogReadResolution(bits)

Parameters

bits : determines the resolution (in bits) of the value returned by


the  analogRead()  function. You can set this between 1 and 32. You can
set resolutions higher than 12 but values returned by  analogRead()  will
suffer approximation. See the note below for details.

Returns

Nothing

Example Code

The code shows how to use ADC with different resolutions.

void setup() {
// open a serial connection
Serial.begin(9600);
}

void loop() {
// read the input on A0 at default resolution (10 bits)
// and send it out the serial connection
analogReadResolution(10);
Serial.print("ADC 10-bit (default) : ");
Serial.print(analogRead(A0));

// change the resolution to 12 bits and read A0


analogReadResolution(12);
Serial.print(", 12-bit : ");
Serial.print(analogRead(A0));

// change the resolution to 16 bits and read A0


analogReadResolution(16);
Serial.print(", 16-bit : ");
Serial.print(analogRead(A0));
// change the resolution to 8 bits and read A0
analogReadResolution(8);
Serial.print(", 8-bit : ");
Serial.println(analogRead(A0));

// a little delay to not hog Serial Monitor


delay(100);
}

Notes and Warnings

If you set the  analogReadResolution()  value to a value higher than your


board’s capabilities, the Arduino will only report back at its highest
resolution, padding the extra bits with zeros.

For example: using the Due with  analogReadResolution(16)  will give you
an approximated 16-bit number with the first 12 bits containing the
real ADC reading and the last 4 bits padded with zeros.

If you set the  analogReadResolution()  value to a value lower than your


board’s capabilities, the extra least significant bits read from the ADC
will be discarded.

Using a 16 bit resolution (or any resolution higher than actual


hardware capabilities) allows you to write sketches that automatically
handle devices with a higher resolution ADC when these become
available on future boards without changing a line of code.

analogWriteResolution()
[Zero, Due & MKR Family]

Description

analogWriteResolution()  is an extension of the Analog API for the


Arduino Due.

analogWriteResolution()  sets the resolution of the  analogWrite()  function.


It defaults to 8 bits (values between 0-255) for backward
compatibility with AVR based boards.
The Due has the following hardware capabilities:

 12 pins which default to 8-bit PWM, like the AVR-based boards.


These can be changed to 12-bit resolution.
 2 pins with 12-bit DAC (Digital-to-Analog Converter)

By setting the write resolution to 12, you can use  analogWrite()  with
values between 0 and 4095 to exploit the full DAC resolution or to set
the PWM signal without rolling over.

The Zero has the following hardware capabilities:

 10 pins which default to 8-bit PWM, like the AVR-based boards.


These can be changed to 12-bit resolution.
 1 pin with 10-bit DAC (Digital-to-Analog Converter).

By setting the write resolution to 10, you can use  analogWrite()  with
values between 0 and 1023 to exploit the full DAC resolution

The MKR Family of boards has the following hardware capabilities:

 4 pins which default to 8-bit PWM, like the AVR-based boards.


These can be changed from 8 (default) to 12-bit resolution.
 1 pin with 10-bit DAC (Digital-to-Analog Converter)

By setting the write resolution to 12 bits, you can


use  analogWrite()  with values between 0 and 4095 for PWM signals;
set 10 bit on the DAC pin to exploit the full DAC resolution of 1024
values.

Syntax

analogWriteResolution(bits)

Parameters

bits : determines the resolution (in bits) of the values used in


the  analogWrite()  function. The value can range from 1 to 32. If you
choose a resolution higher or lower than your board’s hardware
capabilities, the value used in  analogWrite()  will be either truncated if
it’s too high or padded with zeros if it’s too low. See the note below
for details.

Returns

Nothing

Example Code

Explain Code

void setup() {
// open a serial connection
Serial.begin(9600);
// make our digital pin an output
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
}

void loop() {
// read the input on A0 and map it to a PWM pin
// with an attached LED
int sensorVal = analogRead(A0);
Serial.print("Analog Read) : ");
Serial.print(sensorVal);

// the default PWM resolution


analogWriteResolution(8);
analogWrite(11, map(sensorVal, 0, 1023, 0, 255));
Serial.print(" , 8-bit PWM value : ");
Serial.print(map(sensorVal, 0, 1023, 0, 255));

// change the PWM resolution to 12 bits


// the full 12 bit resolution is only supported
// on the Due
analogWriteResolution(12);
analogWrite(12, map(sensorVal, 0, 1023, 0, 4095));
Serial.print(" , 12-bit PWM value : ");
Serial.print(map(sensorVal, 0, 1023, 0, 4095));

// change the PWM resolution to 4 bits


analogWriteResolution(4);
analogWrite(13, map(sensorVal, 0, 1023, 0, 15));
Serial.print(", 4-bit PWM value : ");
Serial.println(map(sensorVal, 0, 1023, 0, 15));

delay(5);
}
Notes and Warnings

If you set the  analogWriteResolution()  value to a value higher than your


board’s capabilities, the Arduino will discard the extra bits. For
example: using the Due with  analogWriteResolution(16)  on a 12-bit DAC
pin, only the first 12 bits of the values passed to  analogWrite()  will be
used and the last 4 bits will be discarded.

If you set the  analogWriteResolution()  value to a value lower than your


board’s capabilities, the missing bits will be padded with zeros to
fill the hardware required size. For example: using the Due with
analogWriteResolution(8) on a 12-bit DAC pin, the Arduino will add 4
zero bits to the 8-bit value used in  analogWrite()  to obtain the 12 bits
required.

noTone()
[Advanced I/O]

Description

Stops the generation of a square wave triggered by  tone() . Has no


effect if no tone is being generated.

Syntax

noTone(pin)

Parameters

pin : the Arduino pin on which to stop generating the tone

Returns

Nothing
Notes and Warnings

If you want to play different pitches on multiple pins, you need to


call  noTone()  on one pin before calling  tone()  on the next pin.

pulseIn()
[Advanced I/O]

Description

Reads a pulse (either  HIGH  or  LOW ) on a pin. For example,


if  value  is  HIGH ,  pulseIn()  waits for the pin to go from  LOW  to  HIGH ,
starts timing, then waits for the pin to go  LOW  and stops timing.
Returns the length of the pulse in microseconds or gives up and
returns 0 if no complete pulse was received within the timeout.

The timing of this function has been determined empirically and will
probably show errors in longer pulses. Works on pulses from 10
microseconds to 3 minutes in length.

Syntax

pulseIn(pin, value)
pulseIn(pin, value, timeout)

Parameters

pin : the number of the Arduino pin on which you want to read the
pulse. Allowed data types:  int .
value : type of pulse to read: either HIGH  or LOW . Allowed data
types:  int .
timeout  (optional): the number of microseconds to wait for the pulse
to start; default is one second. Allowed data types:  unsigned long .

Returns
The length of the pulse (in microseconds) or 0 if no pulse started
before the timeout. Data type:  unsigned long .

Example Code

The example prints the time duration of a pulse on pin 7.

int pin = 7;
unsigned long duration;

void setup() {
Serial.begin(9600);
pinMode(pin, INPUT);
}

void loop() {
duration = pulseIn(pin, HIGH);
Serial.println(duration);
}

pulseInLong()
[Advanced I/O]

Description

pulseInLong()  is an alternative to pulseIn()  which is better at handling


long pulse and interrupt affected scenarios.

Reads a pulse (either  HIGH  or  LOW ) on a pin. For example,


if  value  is  HIGH ,  pulseInLong()  waits for the pin to go from  LOW  to  HIGH ,
starts timing, then waits for the pin to go  LOW  and stops timing.
Returns the length of the pulse in microseconds or gives up and
returns 0 if no complete pulse was received within the timeout.

The timing of this function has been determined empirically and will
probably show errors in shorter pulses. Works on pulses from 10
microseconds to 3 minutes in length. This routine can be used only if
interrupts are activated. Furthermore the highest resolution is
obtained with large intervals.
Syntax

pulseInLong(pin, value)
pulseInLong(pin, value, timeout)

Parameters

pin : the number of the Arduino pin on which you want to read the
pulse. Allowed data types:  int .
value : type of pulse to read: either HIGH  or LOW . Allowed data
types:  int .
timeout  (optional): the number of microseconds to wait for the pulse
to start; default is one second. Allowed data types:  unsigned long .

Returns

The length of the pulse (in microseconds) or 0 if no pulse started


before the timeout. Data type:  unsigned long .

Example Code

The example prints the time duration of a pulse on pin 7.

int pin = 7;
unsigned long duration;

void setup() {
Serial.begin(9600);
pinMode(pin, INPUT);
}

void loop() {
duration = pulseInLong(pin, HIGH);
Serial.println(duration);
}

Notes and Warnings

This function relies on  micros()  so cannot be used


in noInterrupts()  context.
shiftIn()
[Advanced I/O]

Description

Shifts in a byte of data one bit at a time. Starts from either the most
(i.e. the leftmost) or least (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.

If you’re interfacing with a device that’s clocked by rising edges,


you’ll need to make sure that the clock pin is low before the first call
to  shiftIn() , e.g. with a call to  digitalWrite(clockPin, LOW) .

Note: this is a software implementation; Arduino also provides an  SPI


library  that uses the hardware implementation, which is faster but
only works on specific pins.

Syntax

byte incoming = shiftIn(dataPin, clockPin, bitOrder)

Parameters

dataPin : the pin on which to input each bit. Allowed data types:  int .
clockPin : the pin to toggle to signal a read from dataPin.
bitOrder : which order to shift in the bits;
either MSBFIRST or LSBFIRST. (Most Significant Bit First, or, Least
Significant Bit First).

Returns

The value read. Data type:  byte .


shiftOut()
[Advanced I/O]

Description

Shifts out a byte of data one bit at a time. Starts from either the
most (i.e. the leftmost) or least (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.

Note- if you’re interfacing with a device that’s clocked by rising


edges, you’ll need to make sure that the clock pin is low before the
call to  shiftOut() , e.g. with a call to  digitalWrite(clockPin, LOW) .

This is a software implementation; see also the SPI library , which


provides a hardware implementation that is faster but works only on
specific pins.

Syntax

shiftOut(dataPin, clockPin, bitOrder, value)

Parameters

dataPin : the pin on which to output each bit. Allowed data types:  int .
clockPin : the pin to toggle once the dataPin has been set to the
correct value. Allowed data types:  int .
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. Allowed data types:  byte .

Returns

Nothing

Example Code
For accompanying circuit, see the tutorial on controlling a 74HC595
shift register .

//**************************************************************//
// Name : shiftOutCode, Hello World //
// Author : Carlyn Maw,Tom Igoe //
// Date : 25 Oct, 2006 //
// Version : 1.0 //
// Notes : Code for using a 74HC595 Shift Register //
// : to count from 0 to 255 //
//****************************************************************

//Pin connected to ST_CP of 74HC595


int latchPin = 8;
//Pin connected to SH_CP of 74HC595
int clockPin = 12;
////Pin connected to DS of 74HC595
int dataPin = 11;

void setup() {
//set pins to output because they are addressed in the main loop
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
}

void loop() {
//count up routine
for (int j = 0; j < 256; j++) {
//ground latchPin and hold low for as long as you are transmitting
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, j);
//return the latch pin high to signal chip that it
//no longer needs to listen for information
digitalWrite(latchPin, HIGH);
delay(1000);
}
}

Notes and Warnings

The dataPin and clockPin must already be configured as outputs by a


call to pinMode() .

shiftOut is currently written to output 1 byte (8 bits) so it requires a


two step operation to output values larger than 255.

// Do this for MSBFIRST serial


int data = 500;
// shift out highbyte
shiftOut(dataPin, clock, MSBFIRST, (data >> 8));
// shift out lowbyte
shiftOut(dataPin, clock, MSBFIRST, data);

// Or do this for LSBFIRST serial


data = 500;
// shift out lowbyte
shiftOut(dataPin, clock, LSBFIRST, data);
// shift out highbyte
shiftOut(dataPin, clock, LSBFIRST, (data >> 8));

tone()
[Advanced I/O]

Description

Generates a square wave of the specified frequency (and 50% duty


cycle) on a pin. A duration can be specified, otherwise the wave
continues until a call to noTone() . The pin can be connected to a
piezo buzzer or other speaker to play tones.

Only one tone can be generated at a time. If a tone is already playing


on a different pin, the call to  tone()  will have no effect. If the tone is
playing on the same pin, the call will set its frequency.

Use of the  tone()  function will interfere with PWM output on pins 3
and 11 (on boards other than the Mega).

It is not possible to generate tones lower than 31Hz. For technical


details, see Brett Hagman’s notes .

Syntax

tone(pin, frequency)
tone(pin, frequency, duration)

Parameters

pin : the Arduino pin on which to generate the tone.


frequency : the frequency of the tone in hertz. Allowed data
types:  unsigned int .
duration : the duration of the tone in milliseconds (optional). Allowed
data types:  unsigned long .

Returns

Nothing

Notes and Warnings

If you want to play different pitches on multiple pins, you need to


call  noTone()  on one pin before calling  tone()  on the next pin.

You might also like