Pages: [1]   Go Down
Author Topic: What options are there for controlling Arduino using Raspberry Pi?  (Read 10526 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hei,
I´m new to this so any help would be much appreciated.
Im lookinng to controll a couple of LEDS and sensors on the Arduino from the Raspberry Pi.

What Options do I have? Im trying to make this as simple as possible, just wrighting straight phyton code would be great, simple write and read, hopefully without any libarys.

This is somthing like what Im looking for; http://playground.arduino.cc/interfacing/python
but I dont know how to continue this code for example if I want LED on pin 9 to go on, how would that code look?
If anybody has a more extensive guide its most welcome!


I found this but I dont understad if fully, it does look to use some libarys:

http://forum.arduino.cc/index.php?topic=157226.0


This also uses libarys and seem complicated; http://scruss.com/blog/2012/08/14/raspberry-pi-python-arduino/
 Anybody know what type of firmata on the Arduino could work with this? the standardfirmata is huge.

( Also found this but dont know if it works with Raspberry Pi: http://forums.trossenrobotics.com/tutorials/how-to-diy-128/complete-control-of-an-arduino-via-serial-3300/)

« Last Edit: May 31, 2013, 03:55:38 am by vivacatalunya » Logged

Offline Offline
God Member
*****
Karma: 2
Posts: 711
a, b = b, a+b
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

if you use digital sensors, you do not need the arduino.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Could this example help? I had this bookmarked and sitting on my "to try" list for a while...
http://www.doctormonk.com/2012/04/raspberry-pi-and-arduino.html
Logged

Arduino experiments, resources and hacks http://42bots.com

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Im using both analog and digital sensors, the setup is to use a Raspberry Pi and a Arduino im not intrested in changing this.
The Raspberry Pi is meant to override the controls of the Arduino. Arudino runs the controls for all the components and sends the values to the Rapsberry Pi. I see these values on the screen of the Pi, but then I want to be able to change this and override the arduino control with commands from my Pi.

Thank you for the link, It is the kind im intrested in but it is not descriptive enough.

The Dr.monk guide says to write to the Arduino use:
ser.write('5')  // this controls the integrated LED on the Arduino

What I am intrested in is what to write to change for example on/off of the LED on digitial pin 9, or change the temp readings on analog pin 0?

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The example in the link has two programs running:

1) Code uploaded on the Arduino to read from the serial port and do something, based on the data received
2) Code on the Pi to send commands to the Arduino over serial

Quote
What I am interested in is what to write to change for example on/off of the LED on digital pin 9, or change the temp readings on analog pin 0?

Using the Dr. Monk example, to blink an LED on pin 9, instead of 13, you will change this in the Arduino code:
Code:
const int ledPin = 13;

to
Code:
const int ledPin = 9;

If you want to read the value from an analog pin on the Arduino (to get a temperature reading, for example) and send that to the Pi, you will need to reverse the communication. The Arduino will be sending the analog value over serial and the Pi will read it and use it. There some very good tutorials on Arduino and serial communication by Jeremy Bloom:
http://www.youtube.com/course?list=ECA567CE235D39FA84 (Serial Communication starts from #6).
Logged

Arduino experiments, resources and hacks http://42bots.com

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you for the link to the videos.

But you have misunderstood me, this is my fault, I will try to expalin better:
I have the Arduino controlling the LEDS on pin 9 and 10, the sensors on analog 0, and sending this data to the Pi. It is sucsessfully picked up and I can view the values from the Arduino on my Pi.

I have the Arduino code working fine controlling all the components. I now want to be able to send commands from the Raspberry Pi to the Arduino and change the controls/ the value of the components.

So for example; Arduino has code running that tells LED on pin 9 to be Low, and it sends this information to the Pi.
Now on my Pi I want to change the LED on pin 9 to be HIGH, so I would like to write some python code ( or other code if it is easier) to set it HIGH.

Using Dr.monks guide I imagine it would work something like this:

From my Raspberry Pi in python shell sending over serial  to Arduino:

ser.write('LED9 = HIGH')  // set LED on pin 9 on the Arduino to be on.

This could be one solution: http://www.raspberrypi.org/phpBB3/viewtopic.php?f=32&t=37469

But I dont know what the Arduino code would look like.

I tried with this code on my Arduino, but it did not work:

Code:
const int ledPin     = 9;
const int ledPin2   = 10;

void setup(){
  Serial.begin(9600);   
 
}

void loop () {

 
     if (Serial.available()>0) {
      //int d = 0;
      int d= Serial.read();
            if ( d == 1 )
            {
              digitalWrite(ledPin2, HIGH);       
              digitalWrite(ledPin, HIGH);
            }
            else if ( d == 2 )
            {
              digitalWrite(ledPin2, LOW);
              digitalWrite(ledPin, LOW);
            }
           
          }
}
   


I tested this without using the Raspberry Pi. By simply plugging the Arduino into my computer, open the serial monitor and put "1" into the send text field and send it. RX LED on the Arduino flashes when I do this, but the LED does not go on. Anybody know what im doing wrong here?
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 505
Posts: 31332
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Why did you start a new thread. I just answered that in the other thread.
The serial monitor or the Raspberry Pi sends ASCII.
This bit of code reads a single byte from the Pi and does something depending on what that byte is:-
Code:
if(Serial.available() != 0) { // switch on / off sample sending
      char rx = Serial.read();
      switch (rx) {
       case'G': // Go - start sending samples
       active = true;
       break;
       case'S': // Stop - stop sending samples
       active = false;
       break;
       case '2': // make samples from sensors 1 for X and 3 for Y
       upDateLEDs(0x45);
       np = 2;
       break;
       case '3': // make samples from sensors 1 & 2 for X and 3 for Y
       upDateLEDs(0x47);
       np = 3;
       break;
    }   
}
Don't worry about what it does just look at the pattern of reading a byte if one is available and dong something depending on what it s.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry, I thought this thread was getting a bit messy with too much confusin. So i started another one beacuse I really needed this to work today.

Thank you for the reply!

I actually found what I was looking for in the Dr.monk guide. By adding a simple "0" to this line: Serial.read()-'0'; the serial now recives integers.
Full code I used if anybody is intrested:
Code:

const int ledPin     = 9;
const int ledPin2   = 10;
int d=0;

void setup(){
  Serial.begin(9600);   
 pinMode(ledPin,OUTPUT);
 pinMode(ledPin2,OUTPUT);
 digitalWrite(ledPin, HIGH);
}

void loop () {

 
     if (Serial.available()) {
        d= Serial.read()-'0';
            if ( d == 1 )
            {
              digitalWrite(ledPin2, HIGH);     
              digitalWrite(ledPin, HIGH);
            }
            else if ( d == 0 )
            {
              digitalWrite(ledPin2, LOW);
              digitalWrite(ledPin, LOW);
            }
            //delay(100);
          }
}
   


Now you can connect the Raspberry Pi and start writing to the Arduino in the python shell. ser.write('1') turns the LED on, magical!

Thank you all for your help smiley
Logged

Pages: [1]   Go Up
Jump to: