i am using an Arduino Mega board and try to read out an acceleration sensor via I2C and to controll a motor with a PWM Signal from one of the Timer.
My Problem is that the Programm crashes when i use a high speedvalue for the motor while the external motor power is connected. If i disconnect the external motor power the programm runs and reads out the sensor-values. When I disconnect the Sensor from I2C and run the Programm then I can control the motor with the external power supply. Only if I use both the I2C-connected Sensor and the motor with external power the programm stops running.
I already changed the board from an Arduino Uno which i used before and i changed the used Timer for the motor control. But both changes had no effect.
I am not sure if this can be a problem of the Timer interrupts (because Wire-lib uses interrupts and I use a Timer for the motor controll) or if the error is at some other place.
I would be glad if someone has an idea what is going wrong in that application and how to solve it.
Where have you found timer specific code in the Wire library?
My Problem is that the Programm crashes
How does that look like when the Arduino crashes? I just seen it in an endless loop (software error) and resetting all the time (in most cases a power problem) but never saw it crashing and doing nothing.
the crash looks like there is no longer an output in the Serial Monitor. If i use the programm with low speedValues for the motor (like 30 with a max of 400) the the programm runs and gives the wanted results. If i use higher speedvalues then the output on the Serial Monitor stops after a few lines and it is not longer possible to insert new speedvalues to the programm.
The code is:
#include <Shield2.h>
#include <Wire.h>
Shield md;
#define AccAddress 0x53
#define GyroAddress 0x68
int speedValue=0; // speedValue for motor
int speedV =0;
int parseInput() // parse input from Serial to a signed number
{
String inString = "";
int sign =1;
int inChar=0;
do
{
inChar = Serial.read();
if ((char)inChar =='-')
{
sign =-1;
}
if (isDigit(inChar))
{
inString += (char)inChar;
}
if (inChar == '\n')
{
return sign*inString.toInt();
}
Serial.println("reading");
}
while (inChar != '\n');
}
void setup()
{
Wire.begin();
Serial.begin(115200);
md.init();
}
void writeTo(int _dev_address, byte address, byte val) {
Wire.beginTransmission(_dev_address); // start transmission to device
Wire.write(address); // send register address
Wire.write(val); // send value to write
Wire.endTransmission(); // end transmission
}
void loop()
{
writeTo(AccAddress,0x2D,0x08); // Power CTL : ON
writeTo(AccAddress,0x31,1); // DataFormat for acc-Sensor
Wire.beginTransmission(GyroAddress);
Wire.write(0x1D);
Wire.requestFrom(GyroAddress,6);
byte temp[6];
int i=0;
while (Wire.available())
{
temp[i++]=Wire.read();
}
int tempval1 = ((temp[0]<<8) |temp[1]);
int tempval2 = ((temp[2]<<8) |temp[3]);
int tempval3 = ((temp[4]<<8) |temp[5]);
Wire.endTransmission();
Wire.beginTransmission(AccAddress);
Wire.write(0x32);
Wire.requestFrom(AccAddress,6);
i=0;
while (Wire.available())
{
temp[i++]=Wire.read();
}
double tempvala1 = (((temp[1]<<8) |temp[0]));
double tempvala2 = (((temp[3]<<8) |temp[2]));
double tempvala3 = (((temp[5]<<8) |temp[4]));
Wire.endTransmission();
if (Serial.available() > 0)
{
speedValue = parseInput();
}
if (speedValue <-400 || speedValue > 400)
{
speedValue = speedV;
}
else speedV = speedValue;
md.setSpeedv(speedValue);
double a1 = tempvala1*2/65536;
double a2 = tempvala2*2/65536;
double a3 = tempvala3*2/65536;
double a0 = sqrt(a1*a1+a2*a2+a3*a3);
Serial.print(a1);
Serial.print(" | ");
Serial.print(a2);
Serial.print(" | ");
Serial.print(a3);
Serial.print(" | a: ");
Serial.println(a0);
}
The Sensor is connected to the Pins 20 (SDA) and 21 (SCL) to the 3.3V and to GND.
Two problems here:
Your sensor operates on 3V3 and your arduino is giving him 5V on the I2C lines (the Wire library activates the internal weak pullups). Some sensor may be fried by that, your's seem to be quite tolerant but still you have only the weak pullups (if I remember correctly about 20k).
If you insert pullups (2k2) to 3V3 you have the correct value for the sensor but you're at the limit (60-70% of VCC means logical 1, so for 5V it's 3V-3V5).
Putting your motor to higher speed means it consumes more current and probably the voltage drops a bit. This may be enough to disable the internal pullups to put the I2C lines to a HIGH level within an acceptable time frame. A few misses on the SCK line may be enough to let your Arduino wait forever for an answer from your sensor.
thanks for your fast reply. I have one more question about it.
Putting your motor to higher speed means it consumes more current
This is clear...
and probably the voltage drops a bit
Here comes my question. The motor uses an external power supply with high current and voltage. The 5V and 3.3V lines of the arduino board get there power from the USB port of the PC. So i thought the logical circuit of the Arduino-Board should not be affected by the motor current. Is this wrong?
No not inherently, I don't think so but you might check out the IIC hanging thing and insure that your program can't get stuck in a communications loop waiting fir a response. If nothing else check it and fix it if required it would then be one less possible suspect and ... remember you have no real good starting point anyway so most anything could be the villain. Troubleshooting code like anything else is very often eliminating the good guy's until you find the bad one.
The Sensor is connected to the Pins 20 (SDA) and 21 (SCL) to the 3.3V and to GND.
Two problems here:
Your sensor operates on 3V3 and your arduino is giving him 5V on the I2C lines (the Wire library activates the internal weak pullups). Some sensor may be fried by that, your's seem to be quite tolerant but still you have only the weak pullups (if I remember correctly about 20k).
The internal pull-ups are very weak, with external 4k7 pull-ups to 3V3 the bus will function happily at 3V3, since the lines are pulled up to only 3.5V or so. You can also go and remove the pull-up enabling in twi_init() in twi.c if it bothers you. The pull ups are nominally 30k so are too weak to do any damage.
If you insert pullups (2k2) to 3V3 you have the correct value for the sensor but you're at the limit (60-70% of VCC means logical 1, so for 5V it's 3V-3V5).
The Arduino input high threshold is guaranteed <= 0.6 Vdd, so is <= 3.0V - this is compatible with 3V3 CMOS signals (just, lower noise immunity but it works). Table 28-1 in the 48/88/168/328 datasheet. Furthermore with the weak pull ups helping the noise margin is 0.5V rather than 0.3V, so it actually helps!
For most signals from the Arduino I'd agree, but the I2C bus I claim is an exception. The level converter you link to won't work for the I2C bus though, since its not bi-directional. The I2C bus is open-drain specifically to allow easy-interworking with 5 and 3.3V I believe.
There's an app note that speaks directly to this NXP AN10441 (attached) issue and I've used it several time with perfect success. Since the 2 pull-ups must be there for each side if separated you are only adding 2 small mosfet's to a design a BSS138 works well here and I've used 2N7000's with good results.