Reader small image

You're reading from  Arduino Development Cookbook

Product typeBook
Published inApr 2015
Publisher
ISBN-139781783982943
Edition1st Edition
Tools
Concepts
Right arrow
Author (1)
Cornel M Amariei
Cornel M Amariei
author image
Cornel M Amariei

Cornel Amariei is a Romanian inventor and entrepreneur in the fields of Robotics and 3D printing. He has been working with the Arduino platform since its early days in 2007. His past experience involves large cargo gamma ray scanning robotics, ATM security systems, and blind assisting devices. In his spare time, he is a performing musician playing multiple instruments—predominately the guitar. He is also a swimmer, water polo player, and photographer. Over the years, he has built hundreds of Arduino projects, ranging from flying Quadcopters to levitating magnets and underwater robots. Currently, he splits his time between doing his undergraduate studies in electric engineering and computer science at Jacobs University in Bremen, Germany, and his start-ups and research and development job.
Read more about Cornel M Amariei

Right arrow

Chapter 7. Digital Communication with Arduino

In this chapter, we will cover the following recipes:

  • Serial output

  • Controlling the Arduino over serial

  • Software serial and UART between Arduinos

  • Wireless serial

  • I2C between Arduinos

  • SD cards

  • LCD character displays

  • Ethernet

Introduction


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.

Serial output


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.

Getting ready

Just one ingredient is needed for this recipe—an Arduino board connected to a computer via USB.

How to do it…

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...

Controlling the Arduino over serial


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.

Getting ready

There is just one ingredient needed to implement this recipe—an Arduino board connected to a computer via USB.

How to do it…

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...

Software serial and UART between Arduinos


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.

Getting ready

Following are the ingredients needed for this recipe:

  • 2 Arduinos

  • Jumper cables

How to do it…

Follow steps to connect two Arduino UNOs using software serial:

  1. Assuming...

Wireless serial


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.

Getting ready

To execute this recipe, we need the following ingredients:

  • 2 Arduinos

  • Jumper cables

  • One RF link transmitter and RF link receiver pair

How to do it…

The following are the steps to connect two Arduino UNOs using Software Serial over wireless:

  1. Assuming we use pins 8 and 9 for RX and TX on both Arduinos, connect pin 9 on the master Arduino to the DATA pin on the transmitter.

  2. Connect pin 8 on the slave...

I2C between Arduinos


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.

Getting ready

Following are the ingredients needed for this recipe:

  • 2 Arduinos

  • Jumper cables

How to do it…

Follow these steps to connect two Arduino UNOs using I2C:

  1. Connect pins A4 and A5 on one Arduino to the same pins on the other one.

  2. The GND line has to be common for both Arduinos. Connect it with a jumper.

Schematic

Here is a simple implementation. There is no need for a breadboard.

Here is a possible breadboard implementation:

Note

Remember never...

SD cards


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.

Getting ready

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.

How to do it…

Follow these steps to prepare to use an SD card:

  1. Plug the Arduino-compatible SD shield into the Arduino.

  2. Format the SD card to either FAT16 or FAT32.

  3. Insert the SD...

LCD character displays


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.

Getting ready

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

How to do it…

First, we need to connect the monitor to the Arduino. This generally requires a minimum of 6 digital pins as shown in the following diagram:

This is one possible breadboard implementation:

Follow these steps to connect an LCD to the Arduino:

  1. Connect GND to the VSS and R/W pins.

  2. Connect 5V to the Vcc/Vdd input.

  3. Connect 6 digital pins to E (enable), RS, and DB4-DB7. This is the 4-bit way of connecting HD44780-based displays, such as the common LCD character displays.

  4. Lastly, connect a 10K-potentiometer...

Ethernet


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.

Getting ready

The following are the ingredients needed for this recipe:

  • An Arduino

  • An Arduino Ethernet shield

  • A router or just an Ethernet cable

How to do it…

Follow these steps to build the server:

  1. Carefully plug the Ethernet shield into the Arduino.

  2. Connect an Ethernet cable to the shield.

  3. Connect the other end of the Ethernet cable to the same router to which your computer is connected.

Code

The following code attempts to connect to a router using DHCP. Once it does, it will output the IP via serial and then it will wait for incoming...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Arduino Development Cookbook
Published in: Apr 2015Publisher: ISBN-13: 9781783982943
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Cornel M Amariei

Cornel Amariei is a Romanian inventor and entrepreneur in the fields of Robotics and 3D printing. He has been working with the Arduino platform since its early days in 2007. His past experience involves large cargo gamma ray scanning robotics, ATM security systems, and blind assisting devices. In his spare time, he is a performing musician playing multiple instruments—predominately the guitar. He is also a swimmer, water polo player, and photographer. Over the years, he has built hundreds of Arduino projects, ranging from flying Quadcopters to levitating magnets and underwater robots. Currently, he splits his time between doing his undergraduate studies in electric engineering and computer science at Jacobs University in Bremen, Germany, and his start-ups and research and development job.
Read more about Cornel M Amariei