Skip to content
Menu Close
Uncia Robotics Website Logo
  • Home
  • Robotics
  • Arduino
    • Arduino Push Button Debounce using Millis Function
    • Arduino Push Button Switch with LED
    • Arduino PWM – LED Fading using analogWrite
    • Arduino LED Blink Code
  • Toggle website search
Search this website
  • Home
  • Robotics
  • Arduino
    • Arduino Push Button Debounce using Millis Function
    • Arduino Push Button Switch with LED
    • Arduino PWM – LED Fading using analogWrite
    • Arduino LED Blink Code
  • Toggle website search

Contact Us

Categories

  • Arduino
  • Robotics

Address

NH20, Airport Road
City Dharamshala
District Kangra
Himachal Pradesh, India
PIN 176209

Contact Us | 7018093520

Topics

  • Robotics
  • Arduino
  • Home Page

Subscribe

Get all latest content delivered to your email.
Email is required Email is not valid
Thanks for your subscription.
Failed to subscribe, please contact admin.

Follow Us

  • Opens in a new tab
  • Opens in a new tab
  • Opens in a new tab
  • Opens in a new tab
  • Opens in a new tab
© Copyright - Uncia Robotics | All Rights Reserved | Privacy Policy | Terms of Service

Serial.println( )

 Description: Prints data on Serial monitor as a human-readable form followed by a carriage return character and a newline character.Syntax: Serial.print( (val, base);
  • Serial: Serial port object. See the list of available ports for different boards.
  • val: the value to print.| Allowed data types: any
  • base: specifies the number base. (know more)
Returns: number of bytes written, though reading that number is optional. Data type: size_tNotes and Warnings:
  • This command takes the same forms as Serial.print( ).

Prints data to the serial port as human-readable ASCII text. This command can take many forms. Numbers are printed using an ASCII character for each digit. Floats are similarly printed as ASCII digits, defaulting to two decimal places. Bytes are sent as a single character. Characters and strings are sent as-is. For example-

  • Serial.print(78) gives “78”
  • Serial.print(1.23456) gives “1.23”
  • Serial.print('N') gives “N”
  • Serial.print("Hello world.") gives “Hello world.”

An optional second parameter specifies the base (format) to use; permitted values are BIN(binary, or base 2), OCT(octal, or base 8), DEC(decimal, or base 10), HEX(hexadecimal, or base 16). For floating-point numbers, this parameter specifies the number of decimal places to use. For example-

  • Serial.print(78, BIN) gives “1001110”
  • Serial.print(78, OCT) gives “116”
  • Serial.print(78, DEC) gives “78”
  • Serial.print(78, HEX) gives “4E”
  • Serial.print(1.23456, 0) gives “1”
  • Serial.print(1.23456, 2) gives “1.23”
  • Serial.print(1.23456, 4) gives “1.2345”

You can pass flash-memory based strings to Serial.print() by wrapping them with F(). For example:

Serial.print(F("Hello World"))

To send data without conversion to its representation as characters, use Serial.write()

SERIAL_5N1
SERIAL_6N1
SERIAL_7N1
SERIAL_8N1 (the default)
SERIAL_5N2
SERIAL_6N2
SERIAL_7N2
SERIAL_8N2
SERIAL_5E1: even parity
SERIAL_6E1
SERIAL_7E1
SERIAL_8E1
SERIAL_5E2
SERIAL_6E2
SERIAL_7E2
SERIAL_8E2
SERIAL_5O1: odd parity
SERIAL_6O1
SERIAL_7O1
SERIAL_8O1
SERIAL_5O2
SERIAL_6O2
SERIAL_7O2
SERIAL_8O2

Used for communication between the Arduino board and a computer or other devices. All Arduino boards have at least one serial port (also known as a UART or USART), and some have several.

BOARDUSB CDC NAMESERIAL PINSSERIAL1 PINSSERIAL2 PINSSERIAL3 PINS

Uno, Nano, Mini

0(RX), 1(TX)

Mega

0(RX), 1(TX)

19(RX), 18(TX)

17(RX), 16(TX)

15(RX), 14(TX)

Leonardo, Micro, Yún

Serial

0(RX), 1(TX)

Uno WiFi Rev.2

Connected to USB

0(RX), 1(TX)

Connected to NINA

MKR boards

Serial

13(RX), 14(TX)

Zero

SerialUSB

(Native USB Port only)

Connected to

Programming Port

0(RX), 1(TX)

Due

SerialUSB

(Native USB Port only)

0(RX), 1(TX)

19(RX), 18(TX)

17(RX), 16(TX)

15(RX), 14(TX)

101

Serial

0(RX), 1(TX)

 

UART (Universal Asynchronous Receiver / Transmitter) is the part of the circuit responsible for serial communication. In particular, a UART acts as an interface between serial and parallel communications. In fact, on one side we will have an 8-line bus for parallel communication and on the other side two serial outputs RX and TX.

analogRead( )

Description: Reads the value from a specific analog Pin. Arduino boards contain multichannel ADC (Analog to Digital) converter. Furthermore, It has 10 bits of default resolution. For this reason, if you provide 5 Volts of power supply it can divide it into 1024 steps of 0.004.9V or 49 mV each. Below is the list of analog pins in case of Arduino, Nano and Mini.

BoardPWM PinsResolution
Arduino UnoA0, A1, A2, A3, A4, A510 Bit
Mini, NanoA0, A1, A2, A3, A4, A5, A6, A710 Bit

Syntax: analogRead (pin);

  • pin: the Arduino PIN number ( A0 to A5). | Allowed data types: int

Returns: Analog reading on the pin | Allowed data type: int

Notes and Warnings:

  • If the analog input pin is not connected to anything it will return random values.

pinMode( )

Description: Configure specific pin to behave either as input or output.

Syntax: pinMode (pin,MODE);

  • pin: the Arduino pin number to set the mode of.
  • MODE: INPUT, OUTPUT, or INPUT_PULLUP.

Returns: Nothing

Notes and Warnings:

  • It is only applicable to digital pins.

const

const is an abbreviation for the word constant. It’s a Variable qualifier which makes any variable read-only. You can use it like any other variable but you cannot change its value in the program. It’s a good practice to name your pins using a constant qualifier.

digitalWrite( )

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

Syntax: digitalWrite (pin,VALUE);

  • pin: the Arduino pin number.
  • value: HIGH or LOW

Returns: Nothing

Notes and Warnings:

  • It is very important to set pinMode() as OUTPUT first before using digitalWrite() function on that pin.
  • 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.
  • You can use analog Input pins (A0, A1, … etc) as digital pins. With the exception of Arduino Nano, Pro Mini, and Mini’s A6 and A7 pins.

digitalRead( )

Description: Reads digital values from digital pins.

Syntax: digitalRead (pin);

  • pin: the Arduino pin number.

Returns: HIGH or LOW

Notes and Warnings:

  • digitalRead function returns random values (HIGH/LOW) if the input pin is floating (not connected).
  • Analog pins (A0, A1, …etc) can be used as digital pins on Arduino boards except for Arduino Nano and Mini pro.
  • Avoid using pin 13 as an input pin. because of the LED and resistor which are already attached to it.

Serial.begin( )

Description: Starts Serial communication at a given baud rate. To communicate with computer you can use these baud rates: 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, or 115200. You can, however, specify any other baud rate connected at TX and RX pins of Arduino

Syntax: Serial.begin(baud); or Serial.begin(baud, config);

  • Serial: which serial port you are using. Depends on the device you are using.
  • baud: speed in bits per second | Allowed data types: long
  • config: sets data, parity, and stop bits.

Returns: Nothing

Notes and Warnings:

  • Different microcontrollers can have multiple Serial ports. You can use them by calling them with the given name. e.g. In case of Arduino mega we have these four ports:

Serial.begin(9600); Serial1.begin(38400);
Serial2.begin(19200); Serial3.begin(4800);

FTDI stands for Future Technology Devices International. It is a private company which manufactures and design chips which can convert RS-232 or TTL Serial Transmission to USB signals and allow support for legacy devices with modern computers.

delay ( )

Description: Pauses the program for the amount of time (in milliseconds) as specified inside the bracket. (1sec = 1000 ms).

Syntax: delay (ms);

  • ms: the number of milliseconds to pause. Allowed data types: unsigned long

Notes and Warnings:

  • While it is easy to use delay function for short pauses in a program, the use of delay() has significant drawbacks. No other reading of sensors, mathematical calculations, or pin manipulation can go on during the delay function, so in effect, it brings most other activity to a halt.
  • Certain things do go on while the delay function is controlling the Atmega chip, however, because the delay function does not disable interrupts. Serial communication that appears at the RX pin is recorded, PWM (analogWrite) values and pin states are maintained.

analogWrite( )

Description: Writes an analog value (PWM) to the pin. You can use it to change the brightness of an LED or vary the speed of the motor. After calling analogWrite function, the PWM PIN generates a rectangular wave of specified duty cycle.

PWM PinsFrequency
3, 5, 6, 9, 10, 11490 Hz (pins 5 and 6: 980 Hz)

Syntax: digitalWrite (pin,VALUE);

  • pin: the Arduino PIN number. | Allowed data types: int
  • value: a range of values between 0 (Lowest voltage) to 255(Highest Voltage). | Allowed data types: int

Returns: Nothing

Notes and Warnings:

  • In Arduino Uno, the output resolution lies between 0 to 255.
  • Above all there is no need to set pinMode() before using analogWrite( ). The analogWrite( ) function has nothing to with analog pins or analogRead( ).
  • Pin 5 and 6 can have a higher-than-expected duty cycle because of delay() and millis( ) as they share the same timer which is used to generate PWM outputs.

Arduino Data Types

Data Types refer to a System used by the programming language for declaration of variables and functions of different types. In Arduino Microcontroller we use the following Data Types.

voidBooleancharUnsigned charbyteintUnsigned intword
longUnsigned longshortfloatdoublearrayString-char arrayString-object

 

PULSE WIDTH MODULATION (PWM)

PWM or Pulse Width Modulation is used to create a rectangular pulse wave, a signal switched between ON and OFF. This ON and OFF pattern can simulate voltages in between full ON(5V) and Full OFF(0V). The resultant Voltage is called Duty Cycle.

In the graphic below, the vertical dotted lines represent a regular time period. This duration or period is the inverse of the PWM frequency. In other words, with Arduino’s PWM frequency at about 500Hz, the green lines would measure 2 milliseconds each. A call to analogWrite() is on a scale of 0 – 255, such that analogWrite(255) requests a 100% duty cycle (always on), and analogWrite(127) is a 50% duty cycle (on half the time).

arduino pmm

Once you get this example running, grab your Arduino and shake it back and forth. What you are doing here is essentially mapping time across space. To our eyes, the movement blurs each LED blink into a line. As the LED fades in and out, those little lines will grow and shrink in length. Now you are seeing the pulse width.

 

 

Figure-Arduino LED Fading using PWM

Cover Image Arduino LED Blinking on Digital pin 13
Cover Image Arduino LED Blinking on Digital pin 13
Figure Arduino LED Blinking on Digital PIN
Figure: Arduino LED Blinking on Digital PIN 13

Most Arduino boards have a pin connected to an on-board LED in series with a resistor. The constant LED_BUILTIN is the number of the pin to which the on-board LED is connected. Most boards have this LED connected to digital pin 13.

Arduino LED Blink Code