Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
30 views

Arduino Programming Basics

The document provides descriptions and code examples for common Arduino programming functions including pinMode(), setup(), loop(), digitalWrite(), digitalRead(), delay(), analogRead(), tone(), analogWrite(), Serial, and map(). These functions control input/output pins, timing, analog inputs, audio output, serial communication, and value mapping for Arduino programming basics.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Arduino Programming Basics

The document provides descriptions and code examples for common Arduino programming functions including pinMode(), setup(), loop(), digitalWrite(), digitalRead(), delay(), analogRead(), tone(), analogWrite(), Serial, and map(). These functions control input/output pins, timing, analog inputs, audio output, serial communication, and value mapping for Arduino programming basics.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Arduino Programming

Basics
pinMode()
Description The pinMode() method sets the specified digital I/O pin as INPUT or OUTPUT. A
digital or binary I/O pin can have two possible values: HIGH or LOW. Is it possible to set
or read the value of a digital I/O pin by using the digitalWrite() and digitalRead()
methods.

Syntax pinMode(pin, value)

Parameters pin pin number


value INPUT or OUTPUT

Examples:
• pinMode(8, INPUT); // set the pin 8 as an input pin
• pinMode(WLED, OUTPUT); // set the pin WLED as an output pin
• portMode(1, OUTPUT); // set port 1 as an output port
setup()
Description
Function called once when the program begins running. Used to define initial environment
properties such as a pin mode (INPUT or OUTPUT), starting the serial port etc. before the
loop () begins executing. Variables declared within setup () are not accessible within loop().

Syntax void setup() {


statements
}

Parameters statements any valid statements


loop()
Description Continuously executes the lines of code contained inside its block
until the program is stopped. The loop() function is used in
conjunction with setup(). The number of times loop() executes in
each second may be controlled with the delay() and delay
Microseconds() functions.

Syntax loop() {
statements
}

Parameters statements A sequence of statements to be executed over and


over again
digitalWrite()

Description The digitalWrite() method sets the value of a digital output pin.
Possible values are HIGH or LOW.

Syntax digitalWrite(pin,value)

Parameters pin integer value


value HIGH or LOW
digitalRead()

Description The digitalRead() method reads the value of a digital input pin.

Syntax digitalRead(pin)

Parameters pin int: HIGH or LOW


delay()
Description Forces the program to stop running for a specified time. Delay
time are specified in thousandths of a second. The function
call delay(3000) will stop the program for three seconds. Alias
of delay Milliseconds().

Syntax delay(milliseconds)

Parameters milliseconds int: specified as milliseconds


analogRead()

Description The analogRead() method reads the value of an analog


input pin. Possible values range is 0-1023, where 0 is 0 volts and 1023 is
5 volts.

Syntax analogRead(pin)

Parameters pin int: the analog pin number to read


tone()
Description The tone () generates a tone of a determined frequency in hertz
on a specific pin for a specified duration. The tone command uses timer
resources to generate the tone accurately. Use a negative number in
duration for infinite duration. If a previous tone command is playing, and a
new pin is specified, and tone Polyphony has been set to & gt; 1, and there
are available timers, a simultaneous tone will be played on the new pin.

Syntax tone(pin, frequency, duration)


tone(pin, frequency)
Parameters pin int: output pin number in which the tone will
be generated.
analogWrite()

Description The analogWrite() method sets the value of a PWM output pin. By default possible
values range from 0-255. On Wiring v1 boards the PWM capable pins are: 29, 30, 31,
35, 36 and 37. On Wiring S board the PWM capable pins are: 4, 5, 6, 7, 19 and 20.
Note: analogWrite is an alias for the PWMWrite() command.
Doing analogWrite() on a non PWM pin will cause the pin to
be set to HIGH with no other effect.

Syntax analogWrite(pin,value)

Parameters pin int: The PWM output pin number


value int: a value in the range 0-255
Serial
Description
•The Wiring Serial serial port allows for easily reading and writing data to and from external
devices.
•It allows two machines to communicate and gives you the flexibility to make own devices and
use them as the input or output for Wiring programs.
• The serial port used to be a nine pin I/O port that existed on most PCs and can be emulated
through a USB serial adapter.
•The Wiring I/O board has two built-in (hardware) serial ports called Serial and Serial1.
•The port Serial is available through the USB connector in the Wiring hardware.
•The port Serial1 is available on Wiring I/O pins 2(Rx) and 3(Tx).
•It is possible to access the serial port through pins.
•On the Wiring v1 boards it is available on pins 32(Rx) and 33(Tx).
•On the Wiring S board it is available on pins 0(Rx) and 1(Tx).
•Typical baudrates are: 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600 and 115200.
•Tools » Serial ports menu will only list the ports that are currently available.
Methods
• begin() Opens the serial port for reading and writing.
• read() Returns a number between 0 and 255 for the next bytes waiting in the
buffer. Returns -1 if there is no byte
• write() Writes a byte to the serial port.
• print() Writes data (int, float, byte, char, char[], numbers in base (DEC,BIN, OCT or
HEX) or Strings to the serial port.
• println() It works as the print method but prints a new line character at the end of
each call.
• available() Returns the number of bytes available.
• peek() Peek the next byte of data in serial port buffer. This doesn’t retrieve the
actual byte from the buffer. Returns -1 If no data is available.
• flush() Flushes the serial port buffer. calls to read() and available() will return
data received after the flush() command was issued.
• end() Closes the serial port.
map()
Description Re-maps a number from one range to another. In the example above, the
number value is converted from a value in the range 0..255 into a value that
ranges from 0 to 100.Numbers outside the range are not clamped to 0 and 1,
because out-of-range values are often intentional and useful.

Syntax map(value, low1, high1, low2, high2)

Parameters
• value float: The incoming value to be converted
• low1 float: Lower bound of the values current range
• high1 float: Upper bound of the values current range
• low2 float: Lower bound of the values target range
• high2 float: Upper bound of the values target range

You might also like