Arduino - raspberry - i2c

Hello everyone.I have a Raspberry Pi 3 (I use it as a master) , an Arduno Mega (slave) and I use i2c communication to transfer data to my Arduino.
Also,I have a motor .So,using Python3 on my raspberry ,everytime I press '1' (without Enter) I increase the pwm value of motor by 5 (+5) .When I press '0' , I decrease the pwm value by 5 (-5).
I also use an lcd so that I can see the pwm value.
So, the problem is that the maximum value I can send is 125 ,because if I press one more time number '1' there is overflow.
How can I set range between 0-255 and not to 0-125 ?
Here is my code from arduino:

#include <Wire.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd (22,23,24,25,26,27);

int DIR = 2; 
int PWM = 3;
int pwm_value = 0;
char c;
void setup()
{
  Wire.begin(0x8);
  Wire.onReceive(receiveEvent);
}
 
// Function that executes whenever data is received from master
void receiveEvent(int howMany) 
{
  if (Wire.available()) { // loop through all but the last
    c = Wire.read(); // receive byte as a character
  }
}
void loop()
{
  pwm_value = c;
  analogWrite(PWM , pwm_value);
}
from smbus import SMBus
import curses

addr = 0x8 # bus address
bus = SMBus(1) # indicates /dev/ic2-1


give_move = curses.initscr()
curses.noecho()
give_move.nodelay(1) # set getch() non-blocking


num = 0
while 1:

	push_key = give_move.getch()
	if push_key == ord("1"):
		if(num <= 250):
			num += 5
			bus.write_byte(addr, num)
	
	elif push_key == ord("0"):
		if(num >= 5):
			num -= 5
			#bus.write_byte(addr, 0x0) # switch it on
			

Thanks.

Yes well you can’t use the Raspberry Pi as anything else but a master.

The thing that worries me straight off is that a Mega has 10K pull up resistors to 5V and the Pi has 1K8 pull up resistors to 3V3. For safety you need to physically remove the mega’s pull up resistors.

What do you mean by overflow? How does this show itself?

Well,when I press number '1' on keyboard I increase pwm_value (variable) by 5 and when the value is 125 and I press one more time '1' ,I see -1 in my lcd display.

125 working and 130 not working rememebers me to the fact that on I2C the highest adress can only be 127.
Though you try to send a value not the adress.

But maybe the I2C on the Raspi has some limitation to seven bit with sending data too? 2^7 = 128

anyway why did you choose to use I2C? Things would become much more easier if you would use serial. And as your application is
to send another +1 or -1 once every second the speed-requirements are very low.

A raspi should be able to send data as fast as 1 MegaBaud without any problems. Software-serial on any kind of arduino works easy at 9600 baud which would be way long fast enough for this aplication.

best regards Stefan

Hi @arduiNICK .

Initializing to serial do mega no setup () and print in HEX or variable value "c" then this line and veja is correct.

Serial.begin(xxxx);

Serial.println( c,HEX);

RV mineirin

You are declaring the variable 'c' as type 'char' which takes a value between -128 to 127. You really want that variable to be an unsigned char (aka byte) so it can hold the value 0-255. You could also declare it as int to avoid this problem.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.