HELLO THERE!!!
How are you guys? I hope everyone is fine and in good mood always. This week, I wanted to show you guys an experiment I made throughout the weekend. Continuation of last week's experiment, this week, I decided to make a graph out of my output and log the data into a SDcard. Let's get to the specifics without wasting any further time.
HARDWARE:
1.Arduino UNO
How are you guys? I hope everyone is fine and in good mood always. This week, I wanted to show you guys an experiment I made throughout the weekend. Continuation of last week's experiment, this week, I decided to make a graph out of my output and log the data into a SDcard. Let's get to the specifics without wasting any further time.
HARDWARE:
1.Arduino UNO
2.Big Sound Sensor
3. Ethernet Shield/ MicroSD Shield/ Any board with Micro SD slot ( I used and will concentrate the ethernet Shield for this post)
4.MicroSD Card
SOFTWARE:
1.Arduino IDE
2.Processing IDE (download here:https://processing.org/download/)
CIRCUIT ASSEMBLY:
OK, the circuit is assembled as follows:
1. Plug in the Ethernet Shield on top of your Arduino
2. Connect the Big Sound sensor as follows:
SENSOR ARDUINO
A0 -----------> Analog pin A0
G -----------> GND
+ ------------> 5V
The circuit assembly( sorry for the mess :) ) |
BEFORE CODING:
Before coding the Arduino, create a text file in the SD Card. Name the text file anything you wanted (make sure you put the same name in the code). Then, plug in the SD Card to the ethernet shield
CODING THE ARDUINO:
I would like to divide this into two parts:
a)ARDUINO CODE:
#include <SPI.h>
#include <SD.h>
int chipSelect = 4;
int bgsound = A0;
int sensorval;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
Serial.print("Initializing SD card...");
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
}
void loop() {
// make a string for assembling the data to log:
String dataString = "";
// read the big sound sensor ( any sensor you have also can be used here)
bgsound = analogRead(sensorval);
dataString += String(bgsound);
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("bgsound.txt", FILE_WRITE);// Change the txt file name according
//to the txt file in your sdcard
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening text file");
}
}
b)PROCESSING CODE:
import processing.serial.*;
Serial myPort; // The serial port
int xPos = 1; // horizontal position of the graph
float inByte = 0;
void setup () {
// set the window size:
size(800, 500);
// List all the available serial ports
// if using Processing 2.1 or later, use Serial.printArray()
println(Serial.list());
// I know that the first port in the serial list on my mac
// is always my Arduino, so I open Serial.list()[0].
// Open whatever port is the one you're using.
myPort = new Serial(this, "COM2", 9600);
// don't generate a serialEvent() unless you get a newline character:
myPort.bufferUntil('\n');
// set inital background:
background(255,255,255);
}
void draw () {
// draw the line:
stroke(255, 0, 0);//rgb
line(xPos, height-10, xPos, height-10 - inByte);
// at the edge of the screen, go back to the beginning:
if (xPos >= width) {
xPos = 0;
background(255,255,255);
} else {
// increment the horizontal position:
xPos++;
}
}
void serialEvent (Serial myPort) {
// get the ASCII string:
String inString = myPort.readStringUntil('\n');
if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
// convert to an int and map to the screen height:
inByte = float(inString);
println(inByte);
inByte = map(inByte, 0, 1023, 0, height);
}
}
Upload the Arduino Code to your Arduino and then run the Processing code. This is what I get as the output of the experiment:
The Graphical output which reads the Arduino Analog Input. |
The text file in the SD Card. |
A short demo video is shown as below:
Well, that's all from me this week guys. I hope to see you guys next week. Till then, HAPPY TINKERING!!!
Comments
Post a Comment