Category Archives: TUTORIALS

How to Control a Servo’s Angle of Rotation in Relation to a Coordinate

This tutorial explains how to control a Servo’s angle of rotation in relation to a coordinate that can be found in a typical grid of points.  You will need a(n):

  1. Arduino Uno
  2. Breadboard
  3. Servo (that can rotate 360 degrees)
  4. 6 Wires

Set Up 0

Setting It Up

Before we begin to put the parts together, make sure your Arduino board is not connected to your computer!

Set Up 2

First and foremost, the Servo needs to be connected to a proper source of power for it to work.  Make sure to plug in a red wire from the 5V pin to the positive end of the breadboard and to ground it by plugging a black wire from the GND pin to the negative end of the breadboard.

Then, connect your Servo to your breadboard with 3 jumper wires: orange wire (signal) to F8 (breadboard), red wire (+5V) to positive end of breadboard, and brown wire (ground) to negative end of breadboard.

On the same row as the Servo’s orange wire, connect a yellow wire from J8 (breadboard) to pin 9 (Arduino board).

In the end, it should look like this:

Set Up 1

Now, let’s make that Servo rotate!

Arduino Code

You will need to open up the Arduino interface before we can start writing some code.  Remember, your Arduino board is not connected to the computer, so uploading anything is completely impossible.  For your Arduino board’s safety, let’s keep it unplugged for now.

Firstly, you must include two different sets of libraries: (1) Servo.h and (2) math.h.  These two libraries provide you with the ability to use your Servo and to use the inverse tangent function.  Unfortunately, “pi” is not defined in either libraries, including “math.h”, so you must define it yourself.

We will now to declare a function that’s argument relies on the use of coordinates rather than separate x and y variables (see Diagram 1).

C:UsersTessaDocumentsArduinoprojecttutorialDiagram Model
Diagram 1: Coordinate System

Within the loop function, we will state the position of the motor in relation to the grid (see Diagram 1).  Now, we want the motor to angle towards the points located around it (which are diagrammed as circles).  In the code below, we will focus on 4 points (see Diagram 2).

C:UsersTessaDocumentsArduinoprojecttutorialDiagram Model
Diagram 2: Angle of Rotation in relation to Person on Grid

Now plug in your Arduino, upload the code and watch your Servo’s arm rotate!  You can also check out the angle of the arm in relation to the coordinate in your serial monitor!

If you like, you can expand on this tutorial by including sensors into the equation!  Instead of manually giving a set of coordinates, you can collect coordinates through a series of sensors within a built grid.  The motor’s arm will rotate to the individual’s position within that grid, thanks to the sensor!

Detecting occupied space using gridded motion sensors

This tutorial will show how to identify the coordinates of the space encompassed by objects within a grid. By using two axes with evenly spaced PIR motion detectors, multiple moving objects can be identified. In addition, when multiple objects are close together in the space, the coordinates will include any area covered on two sides by the objects themselves.

Note: Although this tutorial will use 3 total PIR detectors, the number of sensors can be expanded for larger grids with minimal changes to the code.

Behavior:

Figure A: Gridded PIR Sensor System

GridTemplate

 

The gridded PIR sensors should be set up as shown in Figure A. In the next few diagrams, the circles represent objects in the grid, and the X’s represent the squares whose coordinates will be returned by Arduino via the serial cable. The red lines show which PIR sensors are set off by the objects.

Figure B: One Object Detected

Grid1Object

If one object is present in the grid, it will set off  PIR sensors attached to the closest column and the closest row.

Figure C: Two objects detected in a line

Grid2Object

If two objects in a straight horizontal/vertical line are detected, each object will be detected and the coordinates returned will only be those where the objects are located.

Figure D: Three objects detected (not in a line)

Grid3Object

If the objects encompass a certain area on two sides (e.g. if a group of people is walking through a gridded area), the space that they enclose will also be returned as a coordinate value.

This will allow for the response to change when a group enters the space. If the group is neatly ordered in a straight line, the returned coordinates will similarly reflect a straight line. However, if the group is disorderly and spread out in the space, the returned coordinates will reflect the total area encompassed by the group.

Figure E: Objects Along a Diagonal Line

GridDiagObject

If the objects are placed along the entire diagonal, then all possible coordinates will be returned. According to the initial behavior, each returned value should be encompassed on two sides. In the diagonal case with all coordinate pairs returned, this is technically true, but some of the squares will be two squares or more removed from the actual object, which may encompass more space than desired. In order to fix this problem, additional sensors, such as force sensors on squares outside the diagonal, will provide enough input to double check the presence of objects in those areas.

This is an optional addition, but for our purposes, we will be focusing only on the PIR motion sensors in this tutorial.

Continue reading Detecting occupied space using gridded motion sensors

Using an air quality sensor

Introduction

In this tutorial we will be connecting a basic air quality sensor to collect data for other applications with Arduino.  There are many sensors you can choose from depending on what gasses or aspect of air quality you are interested in.  We were particularly interested in detecting levels of benzene as an indicator of air pollution caused by combustion engines.

For this tutorial we will be using the MQ-135 sensor which detects levels of: NH3, Benzene, Alcohol, NOx, CO2 and smoke.

For a larger list of sensors check this link

 

 

Parts List

You will need:

  • An Arduino unit.  (we used the Uno)
  • MQ-135 sensor (link to source)
  • 5 – jumper wires
  • Breadboard

IMG_2572

Set Up

When looking at the sensor there are abbreviations next to the pins you need to wire.

G = Ground

AO = Analog Out

DO = Digital Out

V = Voltage (power source)

 

mq-135

 

 

Here is a simple diagram of how your sensor should be wired to your Arduino.

Note:  If you are using this particular sensor board you do not need to use a 10 ohm resistor as some of the basic diagrams for this sensor indicate.

gas sensor_bb

 

Here we have the sensor set up.

IMG_2574

 

Close up of the sensor and the wiring.

IMG_2575

 

For our power source we used the on board 5V output from Arduino.  We wired that to the “V” tab of the sensor board

Next we wired the “G” tab to the ground on the Arduino.

The last step was to wire the Analog Output (AO) to the A0 input on the Arduino.

 

Code

In our code we wanted to float the voltage readings so that we could eventually use it in Processing applications.

We also wanted to convert the voltage readings of 0-1023 from the sensor to a 0.0-5.0 value which would reflect the true voltage being read.

 

Run this code to your Arduino and you will be ready to detect changes in the level of detectable gasses!

 

Notes:  

  • After doing some research about this sensor it was discovered that while the MQ-135 can detect all of the gasses listed above, it cannot distinguish between them.  If you are looking to specifically target one gas, it might be better to find a different sensor.
  • This sensor also needs uses a heater to warm up the sensor.  It has been advised to not use this with a small battery source as it will quickly drain your battery.

Additional Information:

Turn Environmental Light Conditions Into a LED Power Switch

This tutorial allows you to turn environmental data, in this case the amount of light in the setting you are working in, into an on/off switch for LED lights. This tutorial is a precursor to a more complicated setup that will be deployed for turning data gathered by a weather shield into conditions for selecting functions on a Raspberry Pi.

Tutorial Parts List:

  • Arduino UNO (1)
  • Breadboard (1)
  • Photo resistor (1)
  • Jumper wires (7)
  • 10k Ohm resistor (1)
  • 330 Ohm resistor (2)
  • Red LED (1)
  • Yellow LED (1)

Arduino Setup:

Let’s start by setting up the hardware on our Arduino board. Note: the short leg (the negative side) of the LED lights should be below the long leg (the positive side) in this setup. Use the following diagram to guide your setup:

Tutorial_PhotoResistor_bb

 

You may also find the following circuit diagram helpful for your setup:

Tutorial_PhotoResistor_schem

Coding:

Once you have your board set up, it’s time to enter our code into Arduino. What we are looking to accomplish here is getting our Arduino to sense the level of light in the environment, and use that input to determine which LED light should turn on or off. This tutorial could be altered in many ways, you could use only one LED, and simply have that light turn on/off according to light level. You can also alter the level of brightness/darkness Arduino will use to determine whether the LED(s) should be turned on or off.

 

The following video illustrates what you should see when you are finished! I used a small keychain light to demonstrate the change of the LED lights under different light conditions. As mentioned earlier, you can alter the light level needed to switch the LEDs on/off in the code.

Tutorial_PhotoResistor

How To Save Data from Arduino To A .csv File Using Processing

 

Introduction

Spreadsheets are awesome! They are a ubiquitous and powerful file structure that can cause headache and joy in equally heart-stopping intensities. Within the context of a larger project, you may want to save Arduino data (from sensors, for example) to a .csv file for further analysis, visualization, or whatever spreadsheet bonanzas you can dream up. The following two sketches save data from Arduino to a .csv file on your computer, complete with a timestamp with gloriously specific column names (year, month, day, hour, minute, second…and you can add milliseconds!).

This 2-sketch solution takes advantage of the built-in Table class in Processing. Tables are also awesome, as they are basically spreadsheets in the Processing world. Read all about them (this link has some handy methods for working with tables beyond what this tutorialprovides): http://processing.org/reference/javadoc/core/processing/data/Table.html

Here is an overview of what this tutorial accomplishes:

Arduino to .csv graphic

This is a beginning-level tutorial with one prerequisite! Arduino and Processing have to shake hands. I know it’s a lot to ask, but cooperation is at the root of any good spreadsheet. Here’s a link to a tutorial: http://www.hackerscapes.com/2014/10/lab-6-processing/

First, Download and Install the following:

Step 1: Read Data, Send to Processing via Serial Port

First, set up the Arduino side. Here’s a a gist-y sketch:

Step 2: Use Processing to Receive Data from Arduino, Write data to a Table, and Save Table to a .csv
Now we will handle the processing side.

Additional Resources

Using a motion sensor to turn on a light (and keep it on til everybody leaves)

USING A MOTION SENSOR TO TURN ON A LIGHT (and keep it on)

The goal of this tutorial is to use a PIR sensor to detect movement in a plaza, then turn on a secondary system (in this case, just an LED light). The key thing here is not to detect every single movement, but to turn on the light and keep it on until the PIR detects no movement for a certain period of time, at which point the light should turn off.

The parts you’ll need for this tutorial are:

Parts:

  1. PIR motion sensor
  2. Arduino UNO
  3. 3 pin stacking header
  4. Jumper wires + resistors
  5. LED light

Here’s how to set up your breadboard (and resultant circuit):

The PIR sensor has 3 pins, which should be labeled. On our PIR, the pins read
“VCC,” “OUT,” “GND” from left to right on the botton of the plate.

Connect the PIR into the breadboard so that (–) pin connects to the Gnd row,
(+) pin connects to 5V, and Out pin connects to digital pin 2.

To test the sensor, plug the (+) pin of a red LED into pin 13 and connect the (-)
pin to GND

PIR tutorial_bb

PIR tutorial_schem

Now to put in the code. Again, the important thing here is that we don’t just want the PIR sensor to measure motion, we want it to reflect when a space is active and keep the LED on for as long as there is activity in the space. So, we have to set a minimum period of time  after for the PIR to take measurements finding no motion, and then to turn off. In the code, this period is 10 seconds, but you can change it if a longer or shorter amount of time seems more appropriate to you.

Lighting multiple LEDs with sensors (force sensitive resistor or potentiometer)

This tutorial shows how to light multiple LED’s one at a time through the use of either a force sensitive resistor or a potentiometer. Since both components deal with the increase or decrease of resistance through the circuit varying the voltage (which in our case is between 0 and 5v), both can be utilized with the same code.

First and foremost you’re going to need a few parts.

PARTS REQUIRED

Parts required
Parts required

1. Arduino Kit

2. Force Sensitive Reisistor (FSR)

3. Potentiometer

4. 3 – 330 Ohm Resistors

5. 1 – 10 Ohm Resistor

6. Jumper Wires

7. 3 LED’s

8. Breadboard

LIGHTING WITH FORCE SENSITIVE RESISTORS

fsr circuit

Now that we have our parts, we’re going to start putting them together. Let’s start with the force sensitive resistor.

force sensitive resistor circuitry

Connect your force sensitive resistor to the breadboard. From there, place a 10 Ohm resistor as shown in the diagram and connect that to ground by inserting one end of the resistor on the breadboard in the same column as the black wire. On the same row as the resistor insert a jumper wire and connect the other end of the jumper wire to the A0 pin on your arduino board. The ‘A0’ pin is your analogue pin that allows the sensor to communicate with your Arduino chip. Insert a second jumper wire from the sensor to the column of pins on the bread board that connects to the 5v power supply of your arduino kit (red wire).

Now let’s start connecting our LED’s

led

Place an LED on the bread board. Once that is done place a 330 Ohm resistor from the negative end of the LED (shorter leg) to ground and a jumper wire from the positive end of the LED (longer leg) to digital pin 9. We’re using the digital pin because our LED’s are going to operate through information provided from the sensor to the arduino kit digitally.

Let’s do that for a number of LED;s.

3 LED's

So far so good! Now let’s put a code in there and try to light them up.

 

ARDUINO CODE

Let’s open up the arduino interface and start writing some code.  Make sure your arduino is disconnected while coding so nothing burns out or short circuits.

Once you’ve written the code connect your arduino port to the computer and click on the ‘upload’ icon (which is shaped like an arrow) at the top left corner of your interface (right below the edit menu).

arduino

If you run the code before connecting the arduino to your computer you might run into a serial port error.

arduino2

 

If you have gotten everything running smoothly, press on the force sensitive resistor with different pressures and watch the LED’s change.

IMG_0871

 

LIGHTING WITH A POTENTIOMETER

IMG_0862

Now let’s see how we can light up the LED’s using a potentiometer. The thing to note here is that since the code works the same we can repeat all the steps above but replace the force sensitive resistor with the potentiometer.

The circuitry:

potentiometer

 

Insert the potentiometer on to the breadboard and connect jumper wires from the breadboard to the arduino board as shown in the diagram above. The middle jumper wire should be connected to the analog 0 pin as this is going to help determine the varying resistance values that will switch between LED’s. Once the potentiometer is plugged in and you have run the code through arduino, rotate the dial on the potentiometer and watch the LED’s change.

You can potentially develop some very interesting projects with the use of LED’s and sensors especially in an urban context when there is a lot of activity and the circuit is constantly being activated.

Pollution / Wind Sensing + RGB Pixel Strand

Introduction
Pollution has an extremely harmful presence in urban areas, a presence that is almost completely invisible to the human eye.  This project attempts to make the invisible visible.  This tutorial is one piece of a project that senses both wind and pollution and produces in output to a subtle light demonstration of these levels. In this tutorial, I will show how to connect an RGB pixel strand to an Arduino Uno and will provide the code and code instructions for how to receive an input from a sensor and create movement in the light strand.

Parts List
– Arduino Uno
– Adafruit 12mm Digital RGB LED Pixels (Strand of 25 ) WS2801
– 5V power source (computer USB or 5V battery)

Additional Libraries Needed
CLICK HERE  to download the Adafruit WS2801 library needed for this project.

Wiring
Before beginning to wire the lights to the Arduino, please make sure you follow the diagram below and that you are plugging the right end of the light strand to the Arduino.led_pixels_flat-io

Notice that the pixel strand has four colored wires: red, yellow, green, and blue.  First connect the blue wire to the ground (GND).  Then connect the yellow wire to the Digital 2 pin and the green wire to the Digital 3 pin.  Finally, connect the red wire to the 5V regulated power supply. See diagram below.
led_pixels_arduino-wiring


Circuit Diagram

CircuitDiagram


Code

Direct communication between Arduino board and Processing

This tutorial describes the process of direct communication between Arduino and Processing.  I wish to learn this process, in order to overcome  possible serial port problems between Arduino and Processing during serial communication. Moreover, direct control of Arduino board through Processing, results in a more clear and straight forward process. When considering the Arduino library in Processing allow us to get the data from the board automatically with no concerning about pins and its certain data array. This process is enabled by a special library called “StandardFirmata”, that allows data to be transferred automatically from Arduino to Processing by simplifying significantly the initialization of pins and data arrays. First of all, upload the library called “StandardFirmata”, which is located in the examples file of the Arduino sketch window. nj_img_01 nj_img_02 Since the library is uploaded correctly, the further steps of the process do not involve the Arduino window. The next step would be to create an example sketch in Processing in order to demonstrate the process. Open a Processing sketch and follow the code with the embedded comments below. Tutorial: Step A: Blink an LED with digitalWrite function

2014-11-04 20.44.01Click on the Processing sketch to blink an LED 2014-11-04 20.44.29 Click on the Processing sketch to blink an LED T_A Tutorial: Step B: Changing LED brightness with analogWrite function based on mouse position

2014-11-04 20.45.26Changing LED brightness with analogWrite fuction based on mouse position, the left of the sketch of Processing, value = 10 2014-11-04 20.45.36 Changing LED brightness with analogWrite function based on mouse position, Processing window middle position, value = 180 2014-11-04 20.45.45 Changing LED brightness with analogWrite function based on mouse position, Prossesing window right side, value = 255 T_B Tutorial: Step C: Changing RGB LED brightness with analogWrite function based on mouse position

bu_01 2014-11-04 20.46.26 Changing RGB LED brightness with analogWrite function based on handle, value = red 2014-11-04 20.46.39Changing RGB LED brightness with analogWrite function based on handle, value = green  2014-11-04 20.46.56Changing RGB LED brightness with analogWrite function based on handle, value = blue 2014-11-04 20.47.13Changing RGB LED brightness with analogWrite fuction based on handle, value R G B combination T_C   Tutorial: Step D: Analogue Read (Photo Resistor) from Arduino and data visualization in Processing

2014-11-08 17.31.29 Background color is affected by the Photo Resistor input from the Arduino board and is around the value of 50 (0, 255) in this example. 2014-11-08 17.31.34 Background color is affected by the Photo Resistor input from the Arduino board and is around the value of 50 (0, 255) in this example. 2014-11-08 17.31.38 Background color is affected by the Photo Resistor input from the Arduino board and is around the value of 50 (0, 255) in this example T_D Documentation of cc.arduino library  (link http://playground.arduino.cc/Interfacing/Processing) Arduino.list(): returns a list of the available serial devices. If your Arduino board is connected to the computer when you call this function, its device will be in the list.
Arduino(parent, name, rate): create an Arduino object. Parent should be “this” (without the quotes); name is the name of the serial device (i.e. one of the names returned by Arduino.list()); rate is the speed of the connection (typically 57600). Note that in the v2 library, the rate parameter is optional.
pinMode(pin, mode): set a digital pin to input, output, or servo mode (Arduino.INPUT, Arduino.OUTPUT, or Arduino.SERVO). digitalRead(pin): returns the value of a digital pin, either Arduino.LOW or Arduino.HIGH (the pin must be set as an input). digitalWrite(pin, value): writes Arduino.LOW or Arduino.HIGH to a digital pin.
analogRead(pin): returns the value of an analog input (from 0 to 1023).
analogWrite(pin, value): writes an analog value (PWM wave) to a digital pin that supports it (pins 3, 5, 6, 9, 10, and 11); value should be from 0 (always off) to 255 (always on).
servoWrite(pin, value): writes a value to a servo motor; value should be from 0 to 180.

Reference
http://playground.arduino.cc/Interfacing/Processing https://processing.org

Programming an AT tiny84 micro-controler through an Arduino Uno board

This tutorial describes the process of programming an ATtiny84 micro -controller through an Arduino board. The ATtiny84 is a high performance,  small size processor that can be programmed and used instead of an Arduino board. This processor is similar to the original Arduino processor that is embedded on the Arduino Uno board. The use of a series of  ATtinies 45/85 or 44/84 allows multiple connection of sensors or LEDs that exceed substantially the number of Pins that the Arduino Uno board provides. This means that instead of using multiple Arduinos to connect multiple sensors or output devices, we can use multiple ATtinies in order to save space, as well as money. In order to program a series of ATtinies and control the data flow from the Arduino board to them and vice versa, you will need to program each micro-controler individually through the breadboard and store the program to each. In the meantime, you will need to build a circuitry diagram called “bus diagram”, in order to understand the data flow between the Arduino and the multiple ATtinies. The structure of the “bus diagram” is relatively simple. It is a tree hierarchy diagram of Master and Slaves, which means that it does not include network connections between the controllers. The Arduino board is defined as the “Master” controller of the system and the ATtinies as the “Slave” micro-controlers. This means that the Arduino is controlling the flow of data exclusively, by “calling” each ATtiny at a time. Hence, every ATtiny “talks” directly to the Arduino board individually through the Arduino pins.

These diagrams explain the hierarchy of data flow, as well a typical structure of a “bus diagram”.

ATtiny bus diagram

The following steps will describe the process of programming one ATtiny84 micro-controler through the breadboard.

For this process you will be required:

1_  Arduino Uno board

2_ Breadboard

3_  ATtiny84

4_ Capacitor 10uF

5_ 1 LED

6_ 1 resistor 330KΩ

7_ Jumper wires

Step 1

Open a new Arduino IDE (Arduino sketch window). If you don’t have Arduino installed, you can download it from the following link: http://arduino.cc/en/Main/Software

Step 2

In order to program an ATtiny84 you should import the ATtiny boards. To do so, download the ATtiny master zip. by clicking here:  attiny-master

Consult the “Read me” file for accessing the necessary instructions. Unzip the “attiny – master” folder and paste the “attiny” folder in the Arduino sketchbook folder. Inside the Arduino sketchbook folder, create a sub-folder under the name “hardware” and paste the “attiny” folder there.  Your “attiny” folder should contain a text file named “boards” and  a folder named “variants“.

Step 3

Reboot your Arduino IDE. If you go to the “Sketch” menu –> Boards drop down, you should be able to see the names of the new boards that you’ve just installed.

install boards

Step 4

After installing the ATtiny boards you can begin connecting the ATtiny with the Arduino board. You can connect the two through the breadboard.

Before we start listing the connections, we should go through and describe the pins of the ATtiny. There are two kinds of configurations that describe the ATtiny pins, Physical and Software. The Physical Pins and the Pin numbers in the Software are very different. For example, on an ATtiny84, Physical Pin 1 is for VCC while, in the Software, Digital Pin 1 is actually Physical Pin 12. Also, the Analog Pins can have different numbers than the digital pins at the same physical location. The following diagram gathers all the different configurations of the ATtiny84 pins. 

ATtiny 84 Pin Diagram

ATtiny pins

 

Arduino and ATtiny44/84 Connections

 

ATtiny and Arduino

 

Arduino and ATtiny45/85 Connections

schematic ATtiny 85

Step 5

Before you start the wiring connections between the Arduino and the  ATtiny84, you should first set the Arduino as an ISP programmer for the ATtiny. This operation allows the Arduino to program the ATtiny and pass the code to it. To set the Arduino as an ISP programmer you should launch the Arduino IDE and open the examples folder. Menu –> File –> Examples –> Arduino as ISP

Then assign the board that you are using. In our case it is the Arduino Uno board. To do that go to Menu –> Tools –> Board –> Arduino Uno

Make sure you have set the serial port correctly by going to Menu –> Tools –> Serial Port  

Now your Arduino board operates as an ISP programmer.

Step 6

Complete the connections shown in the previous diagrams. In this tutorial we will go through the basic operation of blinking an LED light through the ATtiny board. For this simple operation you will require few more connections to be made. You will need an LED light, a resistor 330KΩ and a Capacitor 10uF. The capacitor is required in order to prevent the Arduino from resetting the bootloader, which we will go through later in this tutorial.

The following photo shows what the final connections should look like.

final connections

Step 7

Open the Blink sketch from the example folder. Menu –> File –> Examples –> Basic –> Blink. In this sketch you will need to change the pin from 13 to 7.

Now you should have 2 Arduino sketches open in your screen

ISP


Blink

Step 8

Now you will set the ATtiny as your new board by going to Menu –> Tools –> Boards –> ATtiny84 1MHz. You can also operate on the ATtiny84 8MHz

Step 9

In this step you will assign the Arduino as ISP as the programmer of the ATtiny. Go to Menu –> Tools –> Programmer –> Arduino as ISP

Arduino as ISP

Step 10

In order to upload the Blink sketch to the ATtiny you should go to Menu –> File –> Upload using Programmer 

Upload using programmer

 

Step 10 

Burn Bootloader. This is not a mandatory step, however it is recommended. To burn the bootloader go to Menu –> Tools –> Burn Bootloader

Burn Bootloader

Notice that the LED starts blinking.  Now the blink sketch is stored in the ATtiny84.

photo-11

Thank you!