Reader small image

You're reading from  C Programming for Arduino

Product typeBook
Published inMay 2013
Reading LevelIntermediate
PublisherPackt
ISBN-139781849517584
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Julien Bayle
Julien Bayle
author image
Julien Bayle

Julien Bayle owns his Master Degree of biology & computer sciences in 2000. After several years in pure IT System Design, he founded Design the Media early 2010 in order to provide his own courses, training & tools for art fields. As a digital artist, he designed some huge new media art installations, like the permanent exhibition of La Maison des Cinématographies de la Méditerranée (Château de la Buzine) in Marseille, France, in 2011. He also works as a new media technology consultant for some private & public entities. As an A/V live performer, he plays his cold electronic music from New York to Marseille, where he actually lives. Arduino framework is one of his first electronic hardware studies early 2005 and he designed the famous protodeck controller with some opensource framework too. As an art & technology teacher, also certified by Ableton in 2010, he teaches a lot of courses about the digital audio workstation named Ableton Live, about the real-time graphical programming framework Max6 and also about Processing and Arduino. As a minimalist digital artist, he works at the crossroads of sound, visual and data. He explores relationships between sounds and visuals through his immersive A/V installations, his live performances and his released music. His work, often described as “complex, intrigating and relevant”, tries to break classical codes to bring audience a new vision of our world through his pure digital and real-time generated stimuli. He's deeply involved in the open source community and loves to share and provide workshops and masterclasses online and on-site too. His personal website is http://julienbayle.net.
Read more about Julien Bayle

Right arrow

Chapter 5. Sensing with Digital Inputs

Arduino boards have inputs and outputs. Indeed, this is also one of the strengths of this platform: to directly provide headers connecting the ATMega chipset legs. We can then directly wire an input or output to any other external component or circuit without having to solder.

In case you need it here, I'm reminding you of some points:

  • Arduino has digital and analog inputs

  • Arduino has digital outputs that can also be used to mimic analog outputs

We are going to talk about digital inputs in this chapter.

We'll learn about the global concept of sensing the world. We are going to meet a new companion named Processing because it is a nice way to visualize and illustrate all that we are going to do in a more graphical way. It is also a pretext to show you this very powerful and open source tool. Then, it will drive us to design the first serial communication protocol between the board and a piece of software.

We'll specifically play with switches, but we will...

Sensing the world


In our over-connected world, a lot of systems don't even have sensors. We, humans, own a bunch of biological sensors directly in and over our body. We are able to feel temperature with our skin, light with our eyes, chemical components with both our nose and mouth, and air movement with ears. From a characteristic of our world, we are able to sense, integrate this feeling, and eventually to react.

If I go a bit further, I can remember a definition for senses from my early physiological courses at university (you remember, I was a biologist in one of my previous lives):

"Senses are physiological capacities that provide data for perception"

This basic physiological model is a nice way to understand how we can work with an Arduino board to make it sense the world.

Indeed, it introduces three elements we need:

  • A capacity

  • Some data

  • A perception

Sensors provide new capacities

A sensor is a physical converter, able to measure a physical quantity and to translate it into a signal understandable...

What does digital mean?


Let's define precisely what the digital term means here.

Digital and analog concepts

Digital, in the computer and electronic worlds, means discrete, which is the opposite of analog/continuous. It is also a mathematical definition. We often talk about domains to define the cases for use of digital and analog.

Usually, the analog domain is the domain related to physical measures. Our temperature can have all the values that are possible and that exist, even if our measuring equipment dosen't have an infinite resolution.

The digital domain is the one of computers. Because of the encoding and finite memory size, computers translates analog/continuous values into digital representations.

On a graph, this could be visualized as follows:

Inputs and outputs of Arduino

Arduino owns inputs and outputs. We can also distinguish analog and digital pins.

You have to remember the following points:

  • Arduino provides digital pins that can be both an input or an output

  • Arduino provides only analog...

Introducing a new friend – Processing


Processing is an open source programming language and Integrated Development Environment (IDE) for people who want to create images, animations, and interaction.

This major open source project was initiated in 2001 by Ben Fry and Casey Reas, two gurus and former students of John Maeda at the Aesthetics and Computation Group at the MIT Media Lab.

It is a programming framework most used by non-programmers. Indeed, it has been designed primarily for this purpose. One of the first targets of Processing is to provide an easy way of programming for non-programmers through the instant gratification of visual feedback. Indeed, as we know, programming can be very abstract. Processing natively provides a canvas on which we can draw, write, and do more. It also provides a very user-friendly IDE that we are going to see on the official website at http://processing.org.

You'll probably also find the term Processing written as Proce55ing as the domain name processing...

Pushing the button


We are going to have fun. Yes, this is the very special moment when we are going to link the physical world to the virtual world. Arduino is all about this.

What is a button, a switch?

A switch is an electrical component that is able to break an electrical circuit. There are a lot of different types of switches.

Different types of switches

Some switches are called toggles. Toggles are also named continuous switches. In order to act on the circuit, the toggle can be pushed and released each time you want to act and when you release it, the action continues.

Some others are called momentaries. Momentaries are named push for action too. In order to act on the circuit, you have to push and keep the switch pushed to continue the action. If you release it, the action stops.

Usually, all our switches at home are toggles. Except the one for the mixer that you have to push to cut and release to stop it, which means it is a momentary.

A basic circuit

Here is a basic circuit with an Arduino...

Playing with multiple buttons


We can extrapolate our previously designed logic with more than one switch.

There are many ways to use multiple switches, and, in general, multiple inputs on the Arduino. We're going to see a cheap and easy first way right now. This way doesn't involve multiplexing a lot of inputs on only a couple of Arduino inputs but a basic one to one wiring where each switch is wired to one input. We'll learn multiplexing a bit later (in the next chapter).

The circuit

Following is the circuit diagram required to work with multiple switches:

Wiring three momentary switches to the Arduino board

The schematic is an extrapolation of the previous one that showed only one switch. We can see the three switches between the +5 V and the three pull-down resistors. Then we can also see the three wires going to digital inputs 2 to 4 again.

Here is a small memory refresh: Why didn't I use the digital pins 0 or 1?

Because I'm using serial communication from the Arduino, we cannot use the digital...

Understanding the debounce concept


Now here is a small section that is quite cool and light compared to analog inputs, which we will dive into in the next chapter.

We are going to talk about something that happens when someone pushes a button.

What? Who is bouncing?

Now, we have to take our microscopic biocybernetic eyes to zoom into the switch's structure.

A switch is made with pieces of metal and plastic. When you push the cap, a piece of metal moves and comes into contact with another piece of metal, closing the circuit. Microscopically and during a very small time interval, things aren't that clean. Indeed, the moving piece of metal bounces against the other part. With an oscilloscope measuring the electrical potential at the digital pin of the Arduino, we can see some noise in the voltage curve around 1 ms after the push.

These oscillations could generate incorrect inputs in some programs. Imagine, that you want to count the states transitions in order, for instance, to run something when...

Summary


We have learnt a bit more about digital inputs. Digital inputs can be used directly, as we just did, or also indirectly. I'm using this term because indeed, we can use other peripherals for encoding data before sending them to digital inputs. I used some distance sensors that worked like that, using digital inputs and not analog inputs. They encoded distance and popped it out using the I2C protocol. Some specific operations were required to extract and use the distance. In this way, we are making an indirect use of digital inputs.

Another nice way to sense the world is the use of analog inputs. Indeed, this opens a new world of continuous values. Let's move on.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
C Programming for Arduino
Published in: May 2013Publisher: PacktISBN-13: 9781849517584
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
Julien Bayle

Julien Bayle owns his Master Degree of biology & computer sciences in 2000. After several years in pure IT System Design, he founded Design the Media early 2010 in order to provide his own courses, training & tools for art fields. As a digital artist, he designed some huge new media art installations, like the permanent exhibition of La Maison des Cinématographies de la Méditerranée (Château de la Buzine) in Marseille, France, in 2011. He also works as a new media technology consultant for some private & public entities. As an A/V live performer, he plays his cold electronic music from New York to Marseille, where he actually lives. Arduino framework is one of his first electronic hardware studies early 2005 and he designed the famous protodeck controller with some opensource framework too. As an art & technology teacher, also certified by Ableton in 2010, he teaches a lot of courses about the digital audio workstation named Ableton Live, about the real-time graphical programming framework Max6 and also about Processing and Arduino. As a minimalist digital artist, he works at the crossroads of sound, visual and data. He explores relationships between sounds and visuals through his immersive A/V installations, his live performances and his released music. His work, often described as “complex, intrigating and relevant”, tries to break classical codes to bring audience a new vision of our world through his pure digital and real-time generated stimuli. He's deeply involved in the open source community and loves to share and provide workshops and masterclasses online and on-site too. His personal website is http://julienbayle.net.
Read more about Julien Bayle