Camera communication with Arduino Mega

Hello!
I want to send a signal to my Arduino Mega as soon as I detect something. It worked fine with 5v, but for some reason, my camera doesn’t work with 5v anymore. So, I switched to 3.3v. The camera worked and it even sent the signal to the board. The arduino board did what it was supposed to do and sent back a signal, when it was done, but the camera didn’t receive anything…

Is that, because the communication only works with 5v? Or is it a problem with my code?


Here my OpenMV code:

	w = min(max(int(blob.w() * 1.2), 10), 160) # Not too small, not too big.
        h = min(max(int(blob.h() * 1.2), 10), 160) # Not too small, not too big.
        x = min(max(int(blob.x() + (blob.w()/4) - (w * 0.1)), 0), img.width()-1)
        y = min(max(int(blob.y() + (blob.h()/4) - (h * 0.1)), 0), img.height()-1)
        out = net.forward(img.binary([(100, 255)]), softmax=True, roi=(x,y,w,h))
        max_idx = out.index(max(out))
        score = int(out[max_idx]*100)
        if (score < 50):
            score_str = "??:??%"
        else:
            score_str = "%s:%d%% "%(labels[max_idx], score)
        img.draw_string(0, 0, score_str, color=(0, 255, 0))
        print(labels[max_idx])
        if (score > 70):
            if (labels[max_idx] == "H"):
                print("HHHHH")
                uart.write("h\r")
                answerH = False
                while (answerH == False):
                    if uart.any():
                        nachrichtVonArduino = uart.readchar()
                        nachrichtenKontrolle = 209 #ID for letter h
                        if (id(nachrichtVonArduino) == nachrichtenKontrolle):
                            print("H ist korrekt!")
                            answerH = True;
            elif (labels[max_idx] == "S") or (labels[max_idx] == "s"): # or (labels[max_idx] == "5"):
                print("SSSSS")
                uart.write("s\r")
                answerS = False
                while (answerS == False):
                    if uart.any():
                        nachrichtVonArduino = uart.readchar()
                        nachrichtenKontrolle = 231 #ID for letter S
                        if (id(nachrichtVonArduino) == nachrichtenKontrolle):
                            print("S ist korrekt!")
                            answerS = True;
            elif (labels[max_idx] == "U") or (labels[max_idx] == "u"): # or (labels[max_idx] == "v"):
                print("UUUUUU")
                uart.write("u\r")
                answerU = False
                while (answerU == False):
                    if uart.any():
                        nachrichtVonArduino = uart.readchar()
                        nachrichtenKontrolle = 235 #ID for letter U
                        if (id(nachrichtVonArduino) == nachrichtenKontrolle):
                            print("U ist korrekt!")
                            answerU = True;

    print(clock.fps())             	# Note: OpenMV Cam runs about half as fast when connected
                                   		# to the IDE. The FPS should increase once disconnected.

Here my arduino code:

// 104: ID for h
// 115: ID for s
// 117: ID for u

// white: TX1
// black: RX1

// yellow: GND
// red: 5v/3.3v

#include <Wire.h>

void setup() {
  // initialize both serial ports:
  Serial.begin(9600);
  Serial1.begin(9600);
  Serial.println("Auf gehts!");
  pinMode(9, OUTPUT);         //LED 
}

void loop() {
  // read from port 1, send to port 0:
  // from Camera to Arduino
  if (Serial1.available()) {
    int inByte = Serial1.read();
    Serial.write(inByte);
    if (inByte == 104)
    {
      for(int i = 0; i < 5; i++)
      {
        digitalWrite(9, HIGH);
        Serial.println("ON");
        delay(500);
        digitalWrite(9, LOW);
        Serial.println("OFF");
        delay(500);
      }
      // h detected, deploying 2 rescue kits
      Serial.println("");
      Serial.println("H detected! Rescue procedure started...");          
      Serial1.write("h");
    }
    else if (inByte == 115)
    {
      for(int i = 0; i < 5; i++)
      {
        digitalWrite(9, HIGH);
        Serial.println("ON");
        delay(500);
        digitalWrite(9, LOW);
        Serial.println("OFF");
        delay(500);
      }
      // s detected, deploying 1 rescue kits
      Serial.println("");
      Serial.println("S detected! Rescue procedure started...");
      Serial1.write("s");
    }
    else if (inByte == 117)
    {
      for(int i = 0; i < 5; i++)
      {
        digitalWrite(9, HIGH);
        Serial.println("ON");
        delay(500);
        digitalWrite(9, LOW);
        Serial.println("OFF");
        delay(500);
      }
      // u detected, deploying 0 rescue kits
      Serial.println("");
      Serial.println("U detected! Rescue procedure started...");
      Serial1.write("u");
    }
  }
}

The camera I use is OpenMV H7, the Arduino board I use is Mega 2560. If you know it is my code, or if you know it is the camera (because it only works with 5v?), then please tell me and help me.

Thank you very much,
Finn

It’s hard to tell by just looking at all the code, I could test it tomorrow and get back to you. In the meantime, you say it used to work I’d double check the wiring, also if you search the forums you’ll find many working Arduino examples try some of them and see if they work.

Yeah, I would appreciate it if you could test it. The connections were like this:

OpenMV to Arduino:
GND to GND
5v to 5v
P4 to RX1
P5 to TX1

The camera should not be connected to any other thing that supplies it with electricity, but the 5v from the arduino board. Also, the camera needs to work without a computer connected (but that shouldn’t be a problem, because it is starting it automatically if the code is saved on the camera and it is turned on through the arduino board, right?)

if you could test my code, maybe with an easier version of just sending letters or numbers to each other, that would be awesome. Because if the wiring and the same algorithm works on your camera and board, I know that either the board or my camera is broken…

Thank you very much,
Finn

Have you already had the chance to test it?


Finn

if you could test my code, maybe with an easier version of just sending letters or numbers to each other

I haven’t had the time yet, it would be helpful if you could provide an simpler example that shows the same issue

Ok, here is an example. As my camera doesn’t work with the communication to Arduino, I can’t check if it is correct. But if it works, then I know, my camera is broken.

OpenMV code:

import sensor, image, time

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)

clock = time.clock()

while(True):
    clock.tick()
    
    if (labels[max_idx] == "H"):
        print("Send signal to Arduino")
        uart.write("h\r")
        answerH = False
        while (answerH == False):
            if uart.any():
                print("Received some signal");
                messageReceived = uart.readchar()
                messageCheck = 209 #ID for letter h
                if (id(messageReceived) == messageCheck):
                    print("Received answer!")
                    answerH = True;

    img = sensor.snapshot()
    print(clock.fps())

And here is the Arduino code:

#include <Wire.h>

void setup() {
  // initialize both serial ports:
  Serial.begin(9600);
  Serial1.begin(9600);
  Serial.println("Auf gehts!");
  pinMode(9, OUTPUT);         //LED 
}

void loop() {
  // read from port 1, send to port 0:
  // from Camera to Arduino
  if (Serial1.available()) {
    int inByte = Serial1.read();
    Serial.write(inByte);
    if (inByte == 104)
    {
      for(int i = 0; i < 5; i++)
      {
        digitalWrite(9, HIGH);
        Serial.println("ON");
        delay(500);
        digitalWrite(9, LOW);
        Serial.println("OFF");
        delay(500);
      }
      Serial.println("");
      Serial.println("H detected! Rescue procedure started...");          
      Serial1.write("h");
    }
  }	
}

Ok, what should happen is the following: The camera sends a signal (‘h’) to the Arduino board. The Arduino board receives it as an ID (104 == h). Then, it would turn on the LED that is connected to pin 9 on and off for 5 seconds. After that, it sends back a signal to the camera (‘h’ again). The camera should also receive it as an ID, but a different one (209). It should then start again. If it ends up in an infinite loop (always sending signals to each other), then it works. If not, the communication might fail because of the code or the wiring.

The wiring should be exactly the same as I explained yesterday.

Thanks for your help,
Finn

Hi, what is labels, where is it defined and where it’s written to ? And this part:

                messageCheck = 209 #ID for letter h
                if (id(messageReceived) == messageCheck):

Do you compare the object id to 209 ? Why would the object ID equal 209 ?
https://docs.python.org/2/library/functions.html#id

I don’t know, but I tried it.

How I came to the result that I would have to use this, is because I printed whatever I received from the Arduino board. This was (for the letter h) 209, for some reason. So I just took this number and in case the received message equals 209, the camera is allowed to go on.

But I mean, you should at least get the message: “Received something”, if the communication works. Do you receive that message when you try the code(s) with the wiring I gave you?


Thanks,
Finn

I just realized that you don’t even initialize UART. Please try the built-in Examples or search the forums for examples and if it doesn’t work let me know and I’ll test it.
Note id() returns the ID of an object (think of it like a memory address) it’s a unique ID for the object, you should compare the char directly it should equal 104 for ‘h’ and 72 for ‘H’.

from pyb import UART

Do you mean this line?

I have it in my other, bigger code. Can you insert this line into the code snippet I sent you earlier today, and then test it for me, please?

As I said, I don’t have another camera. It worked before, but I am not sure if it worked because I was lucky (and it should not have worked). Right now, it doesn’t work, and I am not sure if my camera is broken. That is why I want you to test it with your camera, please.

Please, test it, and tell me if it worked or if it didn’t. Also, insert this line:
import sensor, image, time, os, nn, math

Might be the reason why the upper code doesn’t work.


Thank you for helping me out,
Finn

Do you initialize the serial port and set the baudrate to 9600 ? Like this:

uart = UART(3, 9600, timeout_char = 1000)

I can’t test your code unless you send a working example. Please understand that I have limited time.

Yes, I do (see the first code in my first message).

# Always pass UART 3 for the UART number for your OpenMV Cam.
# The second argument is the UART baud rate. For a more advanced UART control
# example see the BLE-Shield driver.
uart = UART(3, 9600, timeout_char = 1000)

Insert that above clock = time.clock():

import sensor, image, time, os, nn, math
from pyb import UART

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)

# Always pass UART 3 for the UART number for your OpenMV Cam.
# The second argument is the UART baud rate. For a more advanced UART control
# example see the BLE-Shield driver.
uart = UART(3, 9600, timeout_char = 1000)

clock = time.clock()

while(True):
    clock.tick()
    
    if (labels[max_idx] == "H"):
        print("Send signal to Arduino")
        uart.write("h\r")
        answerH = False
        while (answerH == False):
            if uart.any():
                print("Received some signal");
                messageReceived = uart.readchar()
                messageCheck = 209 #ID for letter h
                if (id(messageReceived) == messageCheck):
                    print("Received answer!")
                    answerH = True;

    img = sensor.snapshot()
    print(clock.fps())

Does it work now? It should, right?


Sincerely,
Finn

No it doesn’t, this isn’t defined:

labels[max_idx]

labels and max_idx.

At least run the code before you send it.

OK. Here is the new code. I didn’t have a camera with me, so I couldn’t run the code. But it doesn’t take much time for you to run the code…so everything’s fine, right? Thank you anyway, that you test it for me.

import sensor, image, time, os, nn, math
from pyb import UART

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)

# Always pass UART 3 for the UART number for your OpenMV Cam.
# The second argument is the UART baud rate. For a more advanced UART control
# example see the BLE-Shield driver.
uart = UART(3, 9600, timeout_char = 1000)

clock = time.clock()

while(True):
    clock.tick()
    print("Send signal to Arduino")
    uart.write("h\r")
    answerH = False
    while (answerH == False):
        if uart.any():
            print("Received some signal")
            messageReceived = uart.readchar()
            messageCheck = 209 #ID for letter h
            if (id(messageReceived) == messageCheck):
                print("Received answer!")
                answerH = True

    img = sensor.snapshot()
    print(clock.fps())

I still don’t have a camera with me, so if you could run the code and test it, that would be great. If it works, please test if the communication with the Arduino Mega works. Even if the ID isn’t correct, you should at least get “Received some signal”. If it gives you that, the communication works.

Thanks for your help,
Finn

It looks like it’s working


You should double check your wiring and test the same example, if it still doesn’t work maybe you have damaged your board after all.

OK, thank you very much for your help. I appreciate it!

All the best,
Finn