Arduino and magnetic sensor I2C, Data flow stops

Hi all!
I have two Hall Sensors (LIS3MDL) and one Accelerometer (LIS2DH) connected to my Arduino UNO using I2C.

For my experiments I will only use the first hall sensor at the moment. After I start the program the data flow just stops after a while, even if I do nothing at all and just watch the serial monitor.

Does anyone have a idea why this is happening? Previous google-search did not lead to anything and posting in stack overflow neither… Maybe I am just doing something wrong with my Control Registers? i cannot figure out if the problem is the arduino, the sensor itself or my programming because it has worked before.

Any help is highly appreciated!!

Here is my arduino code:

#include <Wire.h>

#define CTRL_REG1 0x20
#define CTRL_REG2 0x21
#define CTRL_REG3 0x22
#define CTRL_REG4 0x23
#define CTRL_REG5 0x24

//address for first sensor (SDO to ground): 0011100 = 0x1C = 28 (note: only 7 bit)
int ADDRESS = 0x1C; //I2C address of the sensor

int x;
int y;
int z;

void setup(){
  //Serial.println("setting up Sensor...");
  SetupSensor(); // Configure sensor

  delay(1500); //wait for the sensor to be ready 

void loop(){
   getSensorValues();  // This will update x, y, and z with new values


  delay(100); //Just here to slow down the serial to make it more readable

void getSensorValues(){

  byte xMSB = readRegister(ADDRESS, 0x29);
  byte xLSB = readRegister(ADDRESS, 0x28);
  x = (((xMSB&0xFF) << 8) | (xLSB&0xFF));

  byte yMSB = readRegister(ADDRESS, 0x2B);
  byte yLSB = readRegister(ADDRESS, 0x2A);
  y = (((yMSB&0xFF) << 8) | (yLSB&0xFF));

  byte zMSB = readRegister(ADDRESS, 0x2D);
  byte zLSB = readRegister(ADDRESS, 0x2C);
  z = (((zMSB&0xFF) << 8) | (zLSB&0xFF));

int SetupSensor(){

  //enable temperature sensor, high performance mode for x and y axis, 20Hz output data rate,  self test enabled
  writeRegister(ADDRESS, CTRL_REG1, 0b11010101);

  // set fullscale to 16 Gauss, normal mode (Reboot memory content), user register reset function (default)
  writeRegister(ADDRESS, CTRL_REG2, 0b01100000);

  // Continuous conversion mode
  writeRegister(ADDRESS, CTRL_REG3, 0b00000000);

  // CTRL_REG4
  // high performance mode for z axis, LSB at lower address
    writeRegister(ADDRESS, CTRL_REG4, 0b00001000);

  // continuous update for magnetic data
  writeRegister(ADDRESS, CTRL_REG5, 0b00000000);
      // INT_CFG
  // continuous update for magnetic data
  //writeRegister(ADDRESS,0x30, 0b00000000);

void writeRegister(int deviceAddress, byte address, byte val) {
    Wire.beginTransmission(deviceAddress); // start transmission to device 
    Wire.write(address);       // send register address
    Wire.write(val);         // send value to write
    Wire.endTransmission();     // end transmission

int readRegister(int deviceAddress, byte address){

    int v;
    Wire.write(address); // register to read
  Wire.requestFrom(deviceAddress, 1); // read a byte

    while(!Wire.available()) {
        // waiting

   v =;
   return v;


returns a value to indicate succes/failure. so try

int rv = Wire.endTransmission();

in both write and read register. IIRC it should return 0 for OK.

I tried that, it just sends the zeros and the values for some time and then just stops (after 6 zeros, no more value)


I tried a few times more, sometimes it breaks down like this:
rv: 0
rv: 0
rv: 0
rv: 0
rv: 0
rv: 0

and if I try to connect to the second sensor using the address 0x1E it sends rv: 2 for 6 times..

from wire.cpp

// 2:received NACK on transmit of address

When both sensors are connected, can you run an I2C scanner?

what addresses do you get

I get 0x1C (first Hall sensor) and 0x18 (accelerometer which is wired in third place).. the second hall sensor is missing though, I will exchange the wires and stuff, but could that have any influence on the first sensor since I am only trying to communicate to the first one?

EDIT: I exchanged the wires, now all three addresses (0x1C, 0x1E, 0x18) are detected but the program still freezes after some time...