What options are there for controlling Arduino using Raspberry Pi?

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; Arduino Playground - HomePage
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; Raspberry Pi, Python & Arduino – We Saw a Chicken …
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: Trossen Robotics Community Database Error)

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

Could this example help? I had this bookmarked and sitting on my "to try" list for a while...

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?

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

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:
const int ledPin = 13;
to

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:
course - YouTube (Serial Communication starts from #6).

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: Python and Arduino with Pyserial - Raspberry Pi Forums

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

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

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?

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:-

 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.

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:

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 :slight_smile: