Introduction: Read Analog Data Directly in Processing

This instructable presents a fast an easy way to use data received from an analog sensor in Processing. You will learn to utilize the Arduino and prototype electronic boards to read meaningful data from the environment. The sensors can be affected by the light, the orientation, or a user’s physical input. For this matter we will use photocell, accelerometer, and potentiometer.

Step 1: Preparation Steps

1. Upload the “standardFirmata” example from the Arduino IDE in your Arduino board. If you are using Arduino UNO, there is a standard firmata for UNO.

2. Install Arduino library for processing in your libraries folder in the processing sketchbook
For a detailed instructable for this process go to the Arduino playground example

Step 2: Build the Circuits

Choose one of the circuits from the images below depending on your preferred sensor:
1. Accelerometer
2. Potentiometer
3. Photocell

Step 3: Test the Code and Calibrate Sensor Range

1.Use the following code in processing:

import processing.serial.*;
import cc.arduino.*;

Arduino arduino; //creates arduino object

color back = color(64, 218, 255); //variables for the 2 colors

int sensor= 0;
int read;

float value;


void setup() {
  size(800, 600);
  arduino = new Arduino(this, Arduino.list()[0], 57600); //sets up arduino
    arduino.pinMode(sensor, Arduino.INPUT);//setup pins to be input (A0 =0?)
   
    background(back);
}

void draw() {
 
 
  read=arduino.analogRead(sensor);
  background(back);
  println (read);
  value=map(read, 0, 680, 0, width); //use to callibrate  
  ellipse(value, 300,30, 30);
  

}


2. Use the “println” command to output your sensor’s minimum and maximum values. They will be different depending on the context. Plug the min and max values in your map() function.

Step 4: Add Other Variables to Your Code

-Add other variables to manipulate the X and Y axis of the accelerometer. This program tracks down the X and Y values from the accelerometer and displays them altering the position of a graphic in the screen. It needs to be calibrated for both X and Y parameters.

import processing.serial.*;
import cc.arduino.*;

Arduino arduino; //creates arduino object

color back = color(64, 218, 255); //variables for the 2 colors

int xpin= 0;
int ypin= 1;
float value=0;


void setup() {
  size(800, 600);
  arduino = new Arduino(this, Arduino.list()[0], 57600); //sets up arduino
  
 
    arduino.pinMode(xpin, Arduino.INPUT);//setup pins to be input (A0 =0?)
    arduino.pinMode(ypin, Arduino.INPUT);
    background(back);
}

void draw() {
 
  noStroke();
 
 
  ellipse(arduino.analogRead(xpin)-130, arduino.analogRead(ypin)-130,30, 30);//needs to be calibrated for X and Y separately
 
 
  }

Comments

author
rprieto94 made it!(author)2016-03-11

Instead of an accelerometer, I'm using a gyroscope. The circuit is basically the same as an accelerometer. I have a functioning circuit but when combined with processing it is not working. It is reading 0 values

author
akshay.d21 made it!(author)2016-02-06

Hi,

Nice work here.

Can you help me with plotting xyz values on Processing ? I am using an ADXL345. I am quite new to Embedded systems and processing ...

Would appreciate any help ..thanks

author
zac.thefreakenbeast made it!(author)2015-03-12

I am receiving 0 values all across the board when I put the first sample code in.

author
snebtor made it!(author)2015-03-16

Hi Zac,

I think it depends on which accelerometer you are using. The connections might be different. If you are not getting any values it might be that the accelerometer is connected wrong (see image for a different accelerometer).

I made a more detailed manual here: http://snebtor.org/sensing4viz/sensing_for_visuali...

Screen Shot 2015-03-16 at 11.06.18 AM.png
author
acid+ocerin made it!(author)2014-04-10

Here is the link to some information i've found.

I've had the same issue using analogRead(A0);
You can translate the a0 pin to a 14, a1 to 15 and so on until a7 wich is 21.


http://electronics.stackexchange.com/questions/67087/analogread0-or-analogreada0

author
snebtor made it!(author)2014-04-13

Good catch! -Thank you. I got confused when they added the A1 thing