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!

 

Project: Phase 1, How-To

You are going to write a tutorial for this blog on how to do one, little piece of your project. By writing the tutorial, you’ll not only get to know that piece well, you’ll also give back to the Arduino community.

1. With the support of your group, choose one aspect of your project to focus on. Be specific. For example, it’s better to learn how to make a force-sensitive paver (a project need that one group discussed in class), than how to generally use a force sensor.

2. Figure out how to make the code and the circuitry work. Find existing tutorials and fiddle around. If you get stuck, turn to your group members.

The Arduino Playground has links to many, many tutorials. There are also many on Instructables — some are easier to understand than others. What makes for a good tutorial?

3. Write your own tutorial. It must include a parts list, a circuit diagram, code, and any other information you need to make the tutorial as clear as possible (e.g., images, or a brief video).

As you write, think of your fellow classmates: would they understand how to follow your tutorial? Read the Arduino Style Guide for tips on writing tutorials.

4. Post your tutorial on this blog.

  • Find the email with your user name and password (I sent it to you on 10/27 and should have the subject “[HACKERSCAPES] Your username and password”). You have permission to write and edit posts. You can also delete your unpublished posts.  If you need to delete a published post, let me know. (FYI – You can’t view your classmate’s posts that are in draft, and they can’t see yours.)
  • Use the link in the email to log in.
  • Once you’re logged in, click the link on the left side bar for Posts and choose “Add New.”
  • Give your post a clear title.
  • Categorize your post by checking the Tutorials box on the right of the screen.

wordpress_categories

  • To include any images in your tutorial, use the “Add Media” button, located underneath the title of the post.
  • Make your code nice and readable. To to this, you have to wrap the code in an html tag for pre-formatted text, called a pre tag. First, choose the Text tab (see image below).

wordpress_texteditor

  • Once you’re on the Text editor side, copy and paste in your Arduino code into WordPress. On the line before the code starts write: <pre lang=”arduino”>. On the line after the code ends, write: </pre>. If you do this on the Visual editor side, the tags won’t work.
  • Before you publish your draft, you can Preview it to make sure everything looks right. This is a good chance to check that your pre tags are working as planned and look for typos.
  • When you’re ready, hit Publish.
  • Feel free to correct any little things after you publish. If you ever make any more significant updates, it’s good to add a note (like I did at the bottom of Lab 3).

Project: Phase 1, Presentation

You will present your project in class on November 7.  Your presentation will be 10 minutes long, in addition to 5-10 minutes of questions/discussion. You can use whatever supporting materials you would like to use: PowerPoint, boards, the white board, etc.

In your presentation you should:

1. State the larger urban problem/challenge that your project responds to.

2. State why your project addresses this challenge.

3. Explain how your project works.

  • Present a concept diagram of your project. What are your inputs (sensed data)? What are your outputs (responses to the sensed data)?
  • Present a more detailed parts diagram of your project. What hardware will you need? If there are points in the diagram where you’re not decided on the hardware, create a list of choices. For all hardware, include costs.

4. Discuss any additional design or technical questions your team is wrestling with.

Lab 6: Processing

In this lab, you are going to work on Processing, then Arduino, and then Processing and Arduino together. Through the lab, you’ll expand your use of capacitive sensors, practice serial communication, and learn more about Processing’s capabilities.

Processing

1. Complete this short Processing Overview.

2. Poke through the tutorials on processing.org. Look for ones that interest you. (If you’re like me, this will probably make you think — “Oh! So this is how so-and-so made that graphic.”)

Arduino

3. If you didn’t finish the Tiny Piano exercise in class, download my code from bSpace under Resources > Lab 6 Code and look it over.

There’s a few choices I made in my code that may be different from your code. For example, I created the function detectCapSense. It’s not really necessary —  I just wanted to keep loop() as clean as possible.  Also, I chose to store the sensor values in an array. You could store them as integers. It’s just personal style.

I used this code as the starting point for arduino_capsense_controller.ino, which you’ll use in this lab.

4. I made you a game. The code is also available on  bSpace under Resources > Lab 6 Code. Please download it, and then set up your Arduino for capacitive sensing, if it’s not already (the same setup you had in class).

5. Upload arduino_capsense_controller.ino onto your Arduino. Run it. Check your serial monitor to make sure that your capacitive sensors are working. When you’re done, close your serial monitor.

Processing + Arduino

6. Now connect Arduino to Processing. To make sure the connection is working correctly, use the exact same sketch that you used in Lab 4 when you printed “Hello World” with Arduino, and then accessed that “Hello World” with Processing.  I called this sketch processing_readstring.pde on bSpace.

Run processing_readstring.pde. You should see the same numbers that you saw before on your Arduino serial monitor streaming across the Processing serial monitor.

In the image below you see 0,0. If I were touching one of the sensors, there would be a number in the right or left position. For example, 0,60.

processing_capsense_2sensors

If it’s not working, make sure that you are connected to the correct serial port (this will more than likely be the same one you used in Lab 4).  My sketch uses portName = Serial.list()[1] because that is the appropriate port for my computer — update it to work with yours.

7. Now you know that your sensors are working and you are connected to Processing.

Keep your Arduino code going, but stop the previous Processing sketch.  Now run processing_capsense_game.pde. Don’t forget to update the port number — look for portName = Serial.list()[1] .

Use your sensors to control the ball.  So exciting!

8. Actually, this game is lame. Please make it better.

  • Add two more sensors, so you can move the ball up and left, in addition to down and right. (Note that you don’t need alligator clips — you can just touch the end of the wire directly or use a paper clip without plastic coating.)
  • Make 4 additional improvements to the Processing side of the game. You pick what they are.

To see how it’s possible to expand the Processing interface quite a bit, you might derive some inspiration from the example of making your own game of Pong available here.  This is not exactly pretty, since it’s a replica of a 1970s arcade game, but even so, Pong is popular.

Here’s a fancier example of Pong in action:

In general, there are many, many Processing tutorials available online. There is also example code included with the Processing IDE under File > Examples. These examples are pretty fun. They’re also almost entirely self-contained, so you just need to open one, hit Run, and then watch it do its thing.

9. Please upload to bSpace:

  • A paragraph or two explaining what changes you made to the game. (4 points)
  • Your updated Arduino and Processing code. (8 points)
  • A video showing your revised game in action. (8 points)

Environmental Sensing and Responsive Design