Send data from ArduPilot on Pixhawk4 to Arduino nano slave through I2C

Dear all
I need you help in the following issue: I have connected through I2C two ARDUINOs nano. Using the code fromArduino I2C test with Wire and they communicate correctly.
But, when I try to send data from ArduPilot on Pixhawk4 to slave Arduino nano using the following code:

#include <AP_HAL/AP_HAL.h>
#include <AP_HAL_ChibiOS/I2CDevice.h>

#define I2C_ADDR 0x04

using namespace ChibiOS;

void setup();
void loop();

const AP_HAL::HAL &hal = AP_HAL::get_HAL();

AP_HAL::I2CDevice *i2c_dev;

const size_t msg_len = 4;
uint8_t *to_send_i2c, *to_receive_i2c;

uint8_t j = 0;

void setup() {
    i2c_dev = hal.i2c_mgr->get_device( 0, I2C_ADDR ).leak();
    to_send_i2c = new uint8_t[ msg_len ];
    to_send_i2c[ 0 ] = 0x00;
    to_send_i2c[ 1 ] = 0x02;
    to_send_i2c[ 2 ] = 0xF0;
    to_send_i2c[ 3 ] = 0x20;
    to_receive_i2c = new uint8_t[ msg_len ];

void loop() {
    hal.scheduler->delay( 1000 );

    i2c_dev->transfer( to_send_i2c, msg_len, to_receive_i2c, msg_len );


I can’t receive anything on Arduino-nano. Could you please tell me what I should write to send data properly.
Thanking you in advance.

you will need to take the semaphore,

add something like with_semaphore(i2c_dev->get_semaphore()) to your loop. But with out seeing the rest of the code its very hard to say what could be wrong.

I2C is coming to scripting soon-ish, the branch does work if you want to try it, you can then use lua scripting to send and receive without having to get into the main flight code.

Scripting can also already access serial ports if you could that instead. There are a couple of examples

1 Like

Hello Pete
Is there an updated wiki on scripting?

Sorry no real update to the wiki, I’m afraid you have to use the examples and the main code currently. The plan it to auto generate the wiki in the same way the bindings auto generate code. But we haven’t done it yet…

We do have a Scripting section here on discuss and another over on discord, those are the best places to ask questions.

1 Like

Thanks a lot for your answer! This is all my code, I try to run changed example Tools/Hello/Hello.cpp that send number through I2C. And could you please tell me why I need take the semaphore? (I’m noob in hardware programing).

Ah, I assumed you were trying to integrate into the main code base.

Semaphore is just a signal to the i2c bus that you need to use it and no one else can use it until your done.