Showing posts with label rs232. Show all posts
Showing posts with label rs232. Show all posts

Tuesday, April 2, 2013

Drive a DC motor (send data to the arduino)

Exercise 3º Drive a DC motor

The simplest and not for a real use.

Till now, I was communicating from the arduino to the cubieboard, this is quite important, but now, I've to send the information from the cubieboard to the arduino.

For example, I send the amount of milliseconds that the dc-motor must be running.

The circuit is on of the most simplest
You can find all the information here (very good tutorials)
http://www.jeremyblum.com/2011/01/31/arduino-tutorial-5-motors-and-transistors/

I took this schema

http://www.jeremyblum.com/2011/01/31/arduino-tutorial-5-motors-and-transistors/

and here is the circuit, I had to change some values about resistor, transistor, etc. Cause I've to reutilizes some old components

By the moment this circuits just is used to show how we can send information from the cubieboard to the arduino, in the real world I'll make this circuits with Half-H drivers (L293 or similar) like this

And here the code at the arduino, to send the data from cubieboard I used the command
cu -s 115200 -l /dev/ttyS0

Obvious it can be do better, but just is to show the idea.

Wednesday, March 27, 2013

Cubieboard + arduino (thermometer I²C DS1621)

Exercise 2
Temperature sensor.

Now let me introduce to the integrated circuit DS1621, which is a thermometer with a communication system based on a I²C (Integrated-integrated communication)

Our cubieboard also has I²C connection, but I'm still working on a system to send the information from the senses (arduino) to the brain (cubieboard)

This is the video about the construction of all circuit.
I began, doing the circuit to work with the arduino, and checked.
I send the information to the serial, and show it on the screen

After that I connected the serial cable between the arduino y and cubieboard (the cable is an old cd-cable from my old computer)

Here is the video




and here is the result at the cubieboard


There are a lot of information about how to program the arduinos and I²C


I've changed the library "Protocol", cause it's not really necessary a port, cause in the case of I2C its possible to add few of integrated circuirt at the same ports, which will use the same channel of communications


a little bit of code
[CODE TO READ THE DS1621]

#include <Wire.h>
#include <protocol.h>

#define DEV_ID 0x90 >> 1


Protocol protocolTemperature("Temperature");

void setup()
{
  Serial.begin(9600);//I'm using 9600 bps to comunicate the arduino and the cubieboard
  Wire.begin();
  Wire.beginTransmission(DEV_ID); //connect to DS1621
  Wire.write(0xAC); //Access config
  Wire.write(0x02);
  Wire.beginTransmission(DEV_ID); //restart the DS1621
  Wire.write(0xEE); // start conversion
//read the documentation its a very interesting temperature sensor
}

void loop()
{
 protocolTemperature.send(readTemperature());
}
// This will go to another library
//I love the libraries :)
float readTemperature()
{
int8_t firstByte;
int8_t secondByte;
float temp = 0;
 delay(1000); // give time for measurement
 Wire.beginTransmission(DEV_ID);
Wire.write(0xAA); // read temperature command
Wire.endTransmission();
Wire.requestFrom(DEV_ID, 2);    // request two bytes from DS1621 (0.5 deg. resolution)

firstByte = Wire.read();    // get first byte
secondByte = Wire.read();    // get second byte

temp = firstByte;

if (secondByte)    // if there is a 0.5 deg difference
temp += 0.5;
return (temp);
}


[PROTOCOL.cpp]

void Protocol::send(float value)
{
    char buffer[16];
//NOTE: HOW TO CONVERT A FLOAT TO STRING IN ARDUINO
    dtostrf(value,5,2,buffer);
    Serial.println(_id+":"+buffer);
}

[PROTOCOL.h]
//This class will be growing
class Protocol
{
    public:
        Protocol (String id);
        String getId();
        void send(String msg);
        void send(float value);

    private:
        String _id;
};

The circuit to use I²C is quite simple









Sunday, March 24, 2013

Cubieboard + arduino + protocol

Light Sensor

This is another step in the communication between an arduino and a cubieboard.
By te moment I'm only using ttyS0 at the cubieboard, but for more complicated develops should be necessary to use the GPIO

The exercises consist in to detect a value and send it to the cubieboard, sound simple,itsn't ?

Well the circuit is quite simple, its an LDR (light dependent resistor ) connected to an analog connection at the arduino.

With the value of it we change the delay of blink, and with less light the led blink faster.


This is the basic circuit which is a modification from the project 14 (Light sensor from http://math.hws.edu/vaughn/cpsc/226/docs/askmanual.pdf)



Light sensor + serial connection
Well the circuit is just to see how to send the information from the arduino to our cubieboard, and how to interpret the data.

Now it's possible to receive the data and manipulate

There are a little bit of programming at the cubieboard to open the serial connection (ttyS0) ill try to summarize and it could be a bit tedious and

//I searched the code from google, there are a lot of information
//But is there any error or doubt don't hesitate to comment

[CODE FROM CUBIEBOARD]
//Open the serial port TTYS0 = /dev/ttyS0


    fd = open (TTYS0, O_RDWR | O_NOCTTY);
    if (fd < 0) { perror(TTYS0); return (1);}
    tcgetattr(fd,&oldtio);
    memset(&newtio,0, sizeof(struct termios));

//It was really necessary to change the parameters 
//to accommodate the serial from arduino to the serial of cubieboard
// More information here http://www.easysw.com/~mike/serial/serial.html

    newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD;
    newtio.c_iflag = IGNPAR | IGNBRK | IMAXBEL ;
    newtio.c_cc[VTIME]=8;
    tcflush (fd,TCIFLUSH);
    tcsetattr(fd,TCSANOW,&newtio);

//READ
    while (STOP == FALSE)
    {
        res =read (fd,buf,255);
//Eliminate the "intro" of the data
        buf[res-1]=0;
        printf("%s\n",buf);
        if (buf[0]=='z') STOP =TRUE;
    }
    tcsetattr(fd,TCSANOW,&oldtio);
    close(fd);
===================================

For the arduino code I'm making a library, so it will be easy to improve, correct, and share.

[CODE FROM ARDUINO]
//LIBRARY
Protocol::Protocol(int pin)
{
    _pin=pin;
}


void Protocol::send(String msg)
{
    Serial.print(_id +":");
    Serial.println(msg);
}

void Protocol::setId(String id)
{
    _id = id;
}  
//CODE
 #include <protocol.h>

Protocol protocolLight(ledPin);

void setup()
{
  Serial.begin(9600);
  pinMode(ledPin,OUTPUT);
  protocolLight.setId("light");

}

void loop()
{
  lightVal = 1023 - analogRead(ldrPin);
  protocolLight.send(String(lightVal));
  digitalWrite(ledPin,HIGH);



Wednesday, March 20, 2013

Communication test

Exercise 0: Communication test.

Explain.

A serial of leds, will switch on and switch off continuously, in both directions, the delay between switch can be modified using two buttons.
An the delay value will send the cubieboard and displayed.


Code at the arduino.
.....
void setup()
{

//Initiate the serial communications at the arduino
  Serial.begin(115200);

//Digital pins for output
  for (int x=0; x<5;x++)
  {
    pinMode(ledPin[x],OUTPUT);
  }

// Digital pins for Input
  pinMode(btnUP,INPUT); 
  pinMode(btnDOWN,INPUT);
  changeTime =millis();
}

// Main program in a continuous loop
void loop()
{
  if ((millis() -changeTime) > ledDelay)
  {
    changeLED();
    changeTime= millis();
  }
  if (digitalRead(btnUP))
  {

//Serial print will be part of a function to use a protocol
    Serial.println(ledDelay++);
    delay(20);
        if (ledDelay >200) ledDelay =200;
  }

...........

void changeLED()
{
  //switch off all the leds
  for (int x =0; x<5;x++)
  {
    digitalWrite(ledPin[x],LOW);
  }
  digitalWrite(ledPin[currentLed],HIGH);
  currentLed += direction;
  if (currentLed == 4) {direction = -1;}
  if (currentLed == 0) {direction = 1;} 
}


Note: In the "Serial.println(value) will be used to make a function to control the protocol between arduino and cubieboard

To see if the cubieboard can receive the data I use the information that I find here : http://linux-sunxi.org/Cubieboard/TTL

stty -F /dev/ttyS0 -crtscts  
cu -s 115200 -l /dev/ttyS0
 

I know that the circuit is very simple, and even one of the worst I've ever made, but is just to see the idea.

If any one has any idea, or doubt, please share, I made this to learn and share. 

cubieboard + arduino

A brain with senses

Cubieboard its a great system to manage software and even some hardware it has a lot of connections

But If you want an extra power to works in real time such is needed in robotics, drones, 3d Printer, work with sensors, etc, we need an extra.

And arduino give us this power.

Arduino can sense the environment by receiving inputs, and do actions over lights, motors, servos, and other kind of actuators.

Supports analog connections , digital connections.
Low power consume
Exists a lot of shields to work with (list of shields)
A lot of libraries ready to work.
Very easy to program and develop

The idea is to work with a cubieboard for software , arduino to control the hardware and communicate them using serial port


This is the idea one cubieboard - serial cable - arduino
The brain need to know what happens is his legs.

I'm not going to publish to fast as I did at the computer vision exercises, the next exercises will have a part of hardware with arduino and his programming, and software with cubieboard.

I'm still thinking about the communication between them, I'm going to start using serial communication and a I'm going to do my own protocol keep it as simple as possible

I also have to remember how to work with arduinos, it will take a little bit of time

All the exercises are made following this manual 
http://www.EarthshineDesign.co.uk (but this link doesn't exist :S)
so use this one http://math.hws.edu/vaughn/cpsc/226/docs/askmanual.pdf

The next projects will be
0º Communication test
1º Light sensor
2º Temperature sensor
3º Drive a DC motor


For all this projects I don't need the cubieboard, but I'm going to use it to develop the protocol of communications (cubieboard and arduino)

5º Use computer vision to follow a face (for example)

This projects can change meanwhile I improve my knowledge.
Please if any one has a comment, or other idea, will be marvelous to share, and let your comment.