david_1234
Posts: 331
Joined: Mon Jan 01, 2018 2:14 pm

read data from Arduino

Mon Dec 30, 2019 2:53 pm

Hello ,
is there any what to connect Arduino to PI ? not by USB
only with 2 or 3 wires(Tx,RX,GND)?

this is the logic of my question :
I need to read my INA219 sensor , while it connect to a computer that works on 24VDC 300mA(MAX).
no metter what I did - couldn't make it work on PI(try to use 3.3K\4.7 pullups , with 3.3V\5V ,no pullups at all - in all of them stop working after a few hours , couldn't see the the INA219 in

Code: Select all

 i2cdetect -y 1
repalce 6 INA219 - same problem in all of them )
when I connect it to Arduino it works like a charm for days (same wires,same load , same voltage ,same everything)
so I thought maybe I can work with the Arduino read that data , and then send it to the PI read it in my Python code (which then send me the information using the internet) .
I know its very long and complicated way to do this - but this seems like my only option..

in order to save cost I'm using Arduino Pro Mini so I don't have usb connectror.

so is there a way to communicate between Arduino and PI using serial ?(I'm guessing I will need to use RS232-TTL adapter?)

Thanks ,

*** if someone can help with a solution to the i2c INA219 on pi - it will be great!

User avatar
joan
Posts: 14937
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: read data from Arduino

Mon Dec 30, 2019 3:46 pm

If the Pro Mini is running from 3V3 you can just use the UARTs, i.e. just connect grounds, RX to TX, and TX to RX at Pi and Arduino. If the Pro Mini is 5V you will need a resistor divider on the Arduino TX to Pi RX line.

Then write your software to implement any message passing scheme you fancy.

david_1234
Posts: 331
Joined: Mon Jan 01, 2018 2:14 pm

Re: read data from Arduino

Tue Dec 31, 2019 8:07 am

OK - good tho know it's doable :-)
The Arduino is 5V so I have 2 questions,please:
1. where do I need to put the divider? can you show me? and also which values do I need for the 2 resistors?
I forget the formula....
2. I don't need to use any convertor at all(to check the pi is working with my computer I used a TTL-RS232 convertors on pins 6,8,10)?
just connect pin 8,9 in Adruino to pins 8,10 in my PI ?(this the code I wrote and 8,9 is the pins I selected)
Arduino code :

Code: Select all

#include <Wire.h>
#include <Adafruit_INA219.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display

Adafruit_INA219 ina219;
#include <SoftwareSerial.h>
SoftwareSerial SendToPI ( 8, 9); ///tx,Rx
unsigned long time;
void setup(void)
{
  Serial.begin(115200);
  SendToPI.begin(9600);
  while (!Serial)
  {
   delay(1); // will pause Zero, Leonardo, etc until serial console opens
  }
  uint32_t currentFrequency;
  Serial.println("Hello!");
  ina219.begin();
   ina219.setCalibration_32V_1A();
  Serial.println("Measuring voltage and current with INA219 ...");
  lcd.init();                      // initialize the lcd
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("INA219 IS ON!");
  lcd.setCursor(0, 1);
  lcd.print("Measuring Start");
  delay(2000);

}

void loop(void)
{
  float shuntvoltage = 0;
  float busvoltage = 0;
  float current_mA = 0;
  float loadvoltage = 0;
  float power_mW = 0;

  shuntvoltage = ina219.getShuntVoltage_mV();
  busvoltage = ina219.getBusVoltage_V();
  current_mA = ina219.getCurrent_mA();
  power_mW = ina219.getPower_mW();
  loadvoltage = busvoltage + (shuntvoltage / 1000);
  time = millis();

  Serial.print("Running Time(in seconds) - " ); Serial.println(time);
  Serial.print("Bus Voltage:   "); Serial.print(busvoltage); Serial.println(" V");
  Serial.print("Shunt Voltage: "); Serial.print(shuntvoltage); Serial.println(" mV");
  Serial.print("Load Voltage:  "); Serial.print(loadvoltage); Serial.println(" V");
  Serial.print("Current:       "); Serial.print(current_mA); Serial.println(" mA");
  Serial.print("Power:         "); Serial.print(power_mW); Serial.println(" mW");
  Serial.println("");
  lcd.setCursor(0, 0);
  lcd.print("Voltage: ");
  lcd.print(loadvoltage);
  lcd.setCursor(0, 1);
  lcd.print("Current: ");
  lcd.print(current_mA);
  SendToPI.write(current_mA); ///can I send using this command? or do I need something else?
  SendToPI.write("\n");
  delay(1000);
}
and in the PI just connect pins 8,10 and use this part ot read?

Code: Select all

import time
import serial
ser = serial.Serial(
               port='/dev/serial0',
               baudrate = 9600,
               parity=serial.PARITY_NONE,
               stopbits=serial.STOPBITS_ONE,
               bytesize=serial.EIGHTBITS,
               timeout=1
           )
counter=0
while 1:
       x=ser.readline()
       print x
Thanks ,

User avatar
DougieLawson
Posts: 39169
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: read data from Arduino

Tue Dec 31, 2019 8:20 am

Use a 33K and 18K resistor (or 2K & 1K, you can use any pair where one resistor is approx double the other)

Image
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

david_1234
Posts: 331
Joined: Mon Jan 01, 2018 2:14 pm

Re: read data from Arduino

Tue Dec 31, 2019 8:56 am

OK ,
I have try to see what I'm getting without the divider
and it's seem to work
I have connected
ARD PI
8 --> 10
9 --> 8
gnd-->gnd

and when I'm sending

Code: Select all

SendToPI.print(current_mA);
I get the data on my PI
so maybe I don't need it?

Thank you ,

User avatar
joan
Posts: 14937
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: read data from Arduino

Tue Dec 31, 2019 9:03 am

Exposing a Pi GPIO to 5V may eventually kill the GPIO and the Pi. Your Pi - your gamble.

david_1234
Posts: 331
Joined: Mon Jan 01, 2018 2:14 pm

Re: read data from Arduino

Tue Dec 31, 2019 9:10 am

OK , didn't know this .
I thought 5V is also OK

I will add the divider

Thanks !

david_1234
Posts: 331
Joined: Mon Jan 01, 2018 2:14 pm

Re: read data from Arduino

Tue Dec 31, 2019 9:26 am

me again :-)

could this be the reason why my PI won't work with the INA219 ?

if I have operated the INA219 with 5V then the SCL\SDA will also work on 5V?
maybe this was my problem ?
also will it be to work on the same side and connect the INA219 to the pi using the volt divider also?
or am I talking nonsense?

User avatar
joan
Posts: 14937
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: read data from Arduino

Tue Dec 31, 2019 9:35 am

I think it unlikely that using your sensor with pull-ups to 5V would have stopped I2C working on the Pi. It would likely eventually damage the GPIO and the Pi but I'd expect correct readings in the meantime.

david_1234
Posts: 331
Joined: Mon Jan 01, 2018 2:14 pm

Re: read data from Arduino

Tue Dec 31, 2019 11:21 am

I'm sorry butI didn't understand the all post
so let me show you what I did understand :
I think it unlikely that using your sensor with pull-ups to 5V would have stopped I2C working on the Pi
I used 5V pull-ups and everything work for 2 hours , then stop .
It would likely eventually damage the GPIO and the Pi
this is what the problem I have now , INA219 is not working and also the PI in not showing any I2C devices(this is not hte PI I'm working on right now ....)

so just to be sure and start over fresh
if I take brand new PI install buster on it
take a brand new INA219
connect SDA\SCL using 47K Pullups to a 3.3V
everything should work?

because I did it - and it stop working after 1 day , the INA219 can't be read (I'm trying using another PI and also Arduino)

User avatar
joan
Posts: 14937
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: read data from Arduino

Tue Dec 31, 2019 11:38 am

Make sure the module you are using does not have its own pull-ups to 5V. It's safest to power I2C modules from 3V3 when possible.

david_1234
Posts: 331
Joined: Mon Jan 01, 2018 2:14 pm

Re: read data from Arduino

Tue Dec 31, 2019 2:08 pm

it doesn't
no metter I look in all the PDF I have found - didn't say it have a 5V pullups

this is the device I have bought

https://www.adafruit.com/product/904


so you say maybe try to connet it using no pull-ups and 3.3V only?
meaning wire to wire , without any pull-ups\divider ?

Thanks ,

User avatar
joan
Posts: 14937
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: read data from Arduino

Tue Dec 31, 2019 2:19 pm

Yes. That module should work from 3V3. GPIO 2/3 (SDA/SCL) already have pull-ups to 3V3 fitted on the Pi board. You do not need any other pull-ups if you use the standard I2C bus on GPIO 2/3.

david_1234
Posts: 331
Joined: Mon Jan 01, 2018 2:14 pm

Re: read data from Arduino

Tue Jan 07, 2020 1:49 pm

One last question so I will be sure I understand (and avoid any other electric problems):
when I use a I2C device that power from 3.3V the SDA\SCL lines will also work on 3.3V?
so if my "Other side" can read i2c device only from 3.3V it will be OK?(like PI for example)
in that case I won't need any pullups - because I use 3.3V in all the devices? and I can connect SDA-SDA ,SCL-SCL directly?
right?

and if I power-up the I2C device from 5V\12V the SDA\SCL will be 5\12V ? is this correct? in that case I will need to use 3.3V pullups?

Thanks ,

User avatar
rpdom
Posts: 17189
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: read data from Arduino

Tue Jan 07, 2020 7:32 pm

The i2c device shouldn't have any pull-ups. They are on the Pi. The device can be powered from any voltage as long as it doesn't have pull-ups.

How it works is by forcing the SCL and SDA lines down to 0V. The Pi pulls them up to 3.3V when not pulled down. So even if the i2c device was powered by 2000V (not very likely) the SCL and SDA lines won't be pulled up apart from the 3.3V at the Pi end.

Some devices do have on board pull up resistors and they should be removed or disabled if possible.
Unreadable squiggle

Return to “Beginners”