Trying to connect raspberry pi to arduino

Some people probably seen my external board topic. If you haven't, I am trying to control my fan over the internet. I am trying to use a raspberry pi to connect my arduino to the internet, without the use of a bulky shield. I have the arduino connected to the pi through usb and when i use the command

echo a > /dev/ttyACM0

(a is supposed to activate fan speed 3), I can see the rx LED light up, but all that happens is pin 13, a pin that wasnt even specified in pinMode, turns off and on again. The led I have in place of the remote for speed 3 doesnt light up at all. I have also tried I2C and UART pins, with nothing happening.

I have also tried this python script:

import serial
com = serial.Serial('/dev/ttyACM0', 9600)
com.write('97') #ascii code for A as i get errors just using a normal character

And here is my arduino code:

//Initialize Relay value
boolean debug = true;
void pulseOutput(int out, int ms) {
   digitalWrite(out, 1);
   delay(ms);
   digitalWrite(out, 0);
}

boolean relayState = false;
void setup() {
  // initialize serial communication:
  Serial.begin(1200);
  // initialize the LED pins:
    pinMode(12, OUTPUT);
    pinMode(11, OUTPUT);
    pinMode(10, OUTPUT);
    pinMode(9, OUTPUT);
    pinMode(8, OUTPUT);
    pinMode(7, OUTPUT);
}

void loop() {
  // See if there is a char available in serial
  if (Serial.available() > 0) {
    int inByte = Serial.read();
    // do something different depending on the character received.
    // The switch statement expects single number values for each case;
    // in this exmaple, though, you're using single quotes to tell
    // the controller to get the ASCII value for the character.  For
    // example 'a' = 97, 'b' = 98, and so forth:

    switch (inByte) {
      case 1:
        //Fan Speed 3
        if (debug) {
          Serial.println("Fan speed is now at 3");
        }
        pulseOutput(12, 250);
        break;
      case 2:
        //Fan Speed 2
        if (debug) {
          Serial.println("Fan speed is now at 2");
        }
        pulseOutput(11, 250);
        break;
      case 3:
        //Fan Speed 1
        if (debug) {
          Serial.println("Fan speed is now at 1");
        }
        pulseOutput(10, 250);
      break;
      case 4:
        //Fan Speed 0
        if (debug) {
        Serial.println("Fan is now off");
        }
        pulseOutput(9, 250);
      break;
      case 5:
        //Toggle ceiling fan lights
        if (debug) {
        Serial.println("Ceiling lights are toggled");
        }
        pulseOutput(8, 250);
        break;
      case 6:
      // Relay switching
        if (debug) {
          Serial.println("Lamps are now toggled");
        }
        relayState = !relayState;
        if (relayState) {
          digitalWrite(7, 1);
        } else {
          digitalWrite(7, 0);
        }
        break;
      case 7:
        // Force relay to be off
        if (debug) {
          Serial.println("Lamps are now off irreguardless of state");
        }
        
        digitalWrite(7, 0);
        relayState = false;
      break;
    default:
    break;
  }
}
}

I have also tried a self powered usb hub and a 9v dc wall adapter, thinking that not enough power was the problem, still not working. I have also tried using integers instead of ascii values. Nope.

    int inByte = Serial.read();
    // do something different depending on the character received.
    // The switch statement expects single number values for each case;
    // in this exmaple, though, you're using single quotes to tell
    // the controller to get the ASCII value for the character.  For
    // example 'a' = 97, 'b' = 98, and so forth:

    switch (inByte) {
      case 1:

Have you tried printing the value of inByte ?
What have the notes in the comments got to do with what you are actually doing ? You seem to be saying that you are sending lowercase letters but are checking for single digit ints. Is that ever going to work with the code as it is ?

By the way, I found

I am trying to use a raspberry pi to connect my arduino to the internet, without the use of a bulky shield.

a bit ironic as an Arduino shield is less bulky than a Pi.

com.write('97')
Is not the ASCII code for A - it is a string consisting of two bytes 0x39 and 0x37
com.write(97) is what you want here.

You also have to set the baud rate the same at both ends 9600 or 1200 is not the same.

This Python - Arduino demo may be helpful.

It is important for the RPi program to open the serial port, wait for the Arduino to reset and then keep the port open until it is completely finished using the Arduino.

...R

Grumpy_Mike:
You also have to set the baud rate the same at both ends 9600 or 1200 is not the same.

Oh. Well im dumb

Robin2:
This Python - Arduino demo may be helpful.

It is important for the RPi program to open the serial port, wait for the Arduino to reset and then keep the port open until it is completely finished using the Arduino.

...R

Ill try that real quick

So i now have this test code on my arduino:

boolean state = false;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(13, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (Serial.available() > 0) {
    Serial.read();
    state = !state;
    if (state) {
      digitalWrite(13, HIGH);
    } else {
      digitalWrite(13, LOW);
    }
  }
}

This is supposed to toggle pin 13, the LED built in to the board, whenever it receives any serial data. On my computer through the serial monitor, it toggles pin 13 like its supposed to. I can even plug an LED into pin 13. On the pi, it flashes on and off again. I discovered that the same thing happens when I press the reset button. So is the raspberry pi sending something weird that causes the arduino reset? BTW I have no wires in the reset pin.

So is the raspberry pi sending something weird that causes the arduino reset?

Of course it is. Whenever the serial port is opened the Arduino resets, that is how it has always worked.

Grumpy_Mike:
Of course it is. Whenever the serial port is opened the Arduino resets, that is how it has always worked.

So how do I keep it open? Do I have to use c++? I know the language, but only for windows (at least dont know how to compile on linux)

I just discovered that screen /dev/ttyS0 19200,cs8 works! if I press A, it lights up the led! If i press Z, it toggles the relay switching one! now how do i control this from ssh?

beaubeautastic:
So i now have this test code on my arduino:

So, you didn't take your own advice?

Ill try that real quick

...R

Dunno if you noticed:

DIFFERENT BAUD RATES. :slight_smile:

One is 9600 and one is 1200.

Just an observation.

Just an observation.

Which was made in reply#2

Apologies Grumpy......