You're reading from Arduino Development Cookbook
Arduino is not alone in the universe; it can use different digital communication protocols to talk with quite a few other systems. It's one of the great features of the platform; it has all of the standard protocols built in, allowing it to communicate with thousands of different devices.
Digital communication has numerous advantages. It is less susceptible to noise than analog communication, and it usually only requires two lines to communicate to hundreds of devices. This allows communication with the computer, with other microcontrollers such as the Arduino, with the Internet, and even pages to store data.
This is the default for debugging and communication in the Arduino world. Whenever we want to determine what is happening in Arduino, how a sensor is performing or just general code debugging, we can use the serial output functions to write a message to the computer.
Here, we will explore the basics followed by a few tips and tricks on how to write different types of data. An important thing about serial communication on Arduino is that it can only be done between two devices. It is not possible to have three or more devices on the same serial connection.
Just one ingredient is needed for this recipe—an Arduino board connected to a computer via USB.
We just need to connect the Arduino to the computer and begin programming.
The following code will print half a Christmas tree in the serial monitor and then the values of two analog ports, providing the most common types of serial output encountered on Arduino:
void setup(){
// Initialize the Serial communication...
In the Serial output recipe, we've seen how easy it is to print some data from Arduino to the computer. However, this can work the other way. In the serial monitor window in the Arduino IDE, we can write a string and send it to Arduino.
Here, you will learn what to do with that string and how you can use it to control things.
There is just one ingredient needed to implement this recipe—an Arduino board connected to a computer via USB.
Connect Arduino to the computer so that we can start programming it. The following code will start the built-in LED when it receives an 'a'
. It will stop the LED when it receives an 'x'
, and will blink it for a specified amount of time when it receives 'b'
followed by a number from 1 to 9, such as 'b4'
:
int led = 13; void setup(){ pinMode(led, OUTPUT); Serial.begin(9600); } void loop(){ if (Serial.available()){ char com = Serial.read(); // Act according to the value received if (com...
The serial port, professionally called Universal Asynchronous Receiver/Transmitter (UART) communication, is generally used to program and debug the Arduino via the USB port. There are multiple sensors and systems that use UART as the main communication method, and sometimes we need to discuss between two Arduinos to share information, workload, and so on.
However, most Arduinos only have one serial port, which is used by the USB connection. Serial communication can only happen between two devices. What can we do now? With a bit of luck, we'll have an Arduino Mega or similar that has up to four serial ports, but if we don't, there still is a solution. A special library has been written that simulates an UART port on other digital pins. There are a few drawbacks, but it generally works.
Sometimes we just want to cut the wires and send data over air. Believe it or not, it's not that difficult. We can quickly transform a serial or software serial port into a wireless one if we have a wireless transmitter and receiver pair. These wireless components are quite cheap and easy to find. They are available in a few different frequencies, and they are very easy to set up.
We will use the code from the previous recipe, Software serial and UART between Arduinos; except that we will implement the serial over air. There is a catch; we can only send data in one direction.
To execute this recipe, we need the following ingredients:
2 Arduinos
Jumper cables
One RF link transmitter and RF link receiver pair
Maybe sometimes we want to share the workload of one Arduino with another. Or maybe we want more digital or analog pins. Inter-Integrated Circuit or I2C (pronounced I squared C) is the best solution.
I2C is an interesting protocol. It's usually used to communicate between components on motherboards in cameras and in any embedded electronic system.
Here, we will make an I2C bus using two Arduinos. We will program one master Arduino to command the other slave Arduino to blink its built-in LED once or twice depending on the received value.
Follow these steps to connect two Arduino UNOs using I2C:
Connect pins A4 and A5 on one Arduino to the same pins on the other one.
The GND line has to be common for both Arduinos. Connect it with a jumper.
SD cards are great to store data in the long term. Arduino has a library specifically designed to talk to them. With this library, we can create, write, read, and destroy files. This is very handy, especially in data logging applications. We can have an Arduino running for months, recording data, and writing it to the SD card.
In this example, we will read the data from two analog ports and write it to the SD card.
The following are the ingredients needed for this recipe:
An Arduino board connected to a computer via USB.
A formatted SD card; Arduino accepts only FAT16 or FAT32 formatting.
An Ethernet shield or any other Arduino-compatible SD shield.
Optionally, two analog sensors. We will store their values on the SD card. It works without them, but we will only record random values on the analog ports.
There is nothing better than writing any information from the Arduino to a small LCD character display. They are incredibly handy and just look plain cool. Even better, Arduino has a built-in library to do this. Let's explore how we can implement it.
We will need the following ingredients for this recipe:
An Arduino board connected to a computer via USB
An LCD character display of any dimension—16 x 2 is the standard size
A 10K-ohm potentiometer
Jumper wires
Using an Ethernet shield, we can connect an Arduino to the Internet. All the power and awesomeness of the Internet can come to this small blue board we are programming. This is a huge topic; however, the Arduino Ethernet library makes it all simple.
Here, we will create an interesting application for this functionality. We will make the Arduino a local web server to which we can connect to find out the readings of the first three analog inputs, using our browser.
The following are the ingredients needed for this recipe:
An Arduino
An Arduino Ethernet shield
A router or just an Ethernet cable