Sunday, February 28, 2016

Article #18 : Using Arduino To Program an SD Card

Introduction :

Arduino offers the possibility to read from and write to SD cards using the Sd library. However, because the Arduino board doesn’t have a built-in SD card input you have to connect a shield that have an SD card reader into your board. After that, you will be able to use it for a multitude of applications such as reading sensor data values and storing them into the SD card.

In this tutorial, we will see how to connect the SD card shield to the microcontroller, write some basic commands of the SD library and finally write a program creating a file, writing data into it and reading it after.

Project parts :

  •         Arduino Uno.
  •           SD card shield.


SD card shield pinout :

First, you need to connect the shield to the Arduino board. On every pin of the shield, there is a label. S, you have to connect avery pin of the shield (+5v, GND, SCK, MOSI, MISO) to its corresponding pin on the Arduino card, according to the documentation of Arduino Uno :

  •        MOSI è Pin 11
  •        MISO è Pin 12
  •        SCK   è Pin 13
  •        SS      è Pin 10

Then, connect the Cs pin on any of the remaining digital I/O pins of your Arduino, in this tutorial, I have connected it to pin 5.
You also have to format the SD card to FAT16 or FAT32 from your computer.


Basic Codes :

As always, you have to include the library

#include <SD.h>

To create a file you have to create a variable of type File.

File file;

Then, in the setup function, you have to write a code to start the SD card. The following sequence of code enables to determine if there is a connection error via the serial bus.

If(!SD.begin(5)) {
          Serial.println(“Starting SD card error!”);
}
Serial.println(“SD card ready.”);

Using the File variable that we have created, we can open the file in read or write mode.

file = SD.open(“myFile.txt”, FILE_WRITE);//file on writing mode
file = SD.open(“myFile.txt”, FILE_READ);//file on read mode

We usually have to check if the file has been opened, for that we use the following condition :

if (file) {
          Serial.print(“File opened successfully”);
}else {
          Serial.println(“error while opening the file”);
}

We can then write to or read from the created file depending on the defined mode.

file.println(“Welcome to simple-arduino tutorial !”);
file.close();//always remember to close the file after

In read mode, we create a loop that reads the file until its end.

While(file.available()) {
          char c = file.read();
          Serial.write(c);//send the character to serial monitor
}


Total Code:

The following code uses all functions that we have seen above. It writes to a file, then sends the data over serial bus to the screen.

#include <SD.write>

File file;

void setup() {

          Serial.begin(9600);
          Serial.print(“Initializing SD card”);
          pinMode(10, OUTPUT);
         if (!SD.begin(5)) {
                   Serial.println("initialization failed!");
                   return;
         }

         Serial.println("initialization done.");
         file = SD.open("test.txt", FILE_WRITE);

// if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    // close the file:

    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
 
  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");
   
    // read from the file until there's nothing else in it:
    while (myFile.available()) {
        Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

void loop()
{
    // all programming happens in setup mode
}
 



Sunday, February 21, 2016

ARTICLE #17 : Using Arduino with Processing

Introduction :

For writing programs and loading them to the Arduino board, we used the Arduino IDE program. Now we will see another programming language and IDE to communicate with Arduino using serial port which is called Processing.

Processing is simply an open source computer programming language and IDE built for the electronic arts using a visual context. The processing language is based on the Java language. So the programs that we are going to write are similar to Java code.

In this tutorial we will see :

  •         How to send data from Android and receive it in Processing.
  •         How to send data from processing and receive it in Android.

To perform this tutorial you will have to have:

  •         Arduino IDE download it from here.
  •         Processing IDE download it from here.



Hello world :

We will write a “hello world” to the Arduino board using Arduino IDE and then manage to receive it in the Processing IDE.


Code Arduino :

Void setup() {
            Serial.begin(9600); //initializing serial communications
}
Void loop() {
            Serial.println(“hello world”); //wait 500 ms and write it again
}


Code Processing :

To receive data in Processing we have to create an object from serial class that will get the communication port that Arduino is hooked to.

import processing.serial.*;

Serial myPort;
String val; //Data received from the serial port

void setup() {

            String portName = Serial.list()[0];//depending on the port number of the device
            myPort = new Serial(this, portName, 9600);
}

void draw() {

            if(myPort.available() > 0) { //If data is available
                        val = myPort.readStringUntil(‘\n’);//read the string and put it in val
                        If(val != null)
                                    Println(val);
            }

}


Now that we learned how to receive data sent from Arduino to Processing. We will now learn how to do it the other way, from processing to Arduino.
What does the following code ? whenever we press the mouse, processing sends ‘1’ to Arduino. Whenever the Arduino IDE catches ‘1’ it turns the LED pin 13 on.


Code Processing :


import processing.serial.*;
Serial myPort; //Creating a Serial object

void setup() {

            size(200, 200);//The space for the mouse to click
            String portName = Serial.list()[0];
            myPort = new Serial(this, portName, 9600);

}
void draw() {

            if(mousePresses == true) {
                        myPort.write(‘1’);//send 1 over the serial port
                        println(“1”);//printing it to make sure we are sending 1
                        } else {
                        myPort.write(‘0’); //send 0 instead
                        }

}

Code Arduino :


char cal;
int ledPin = 13;

void setup() {

            pinMode(ledPin, OUTPUT);
            Serial.begin(9600);

}

void loop() {

            if(Serial.available()) {

                        val = Serial.read();

            }
if (val == ‘1’) {

            digitalWrite(ledPin, HIGH);
            } else {
            digitalWrite(ledPin, LOW);

}

delay(10);
}



When we load the code above to Arduino and run Processing sketch, you should be able to turn the LED on by simply clicking into the created processing canvas.