Problem letting multiple sensors work at the same time. SOLVED

Hello,

I am building a weatherstation with an arduino Mega. The problem i have is that when i combine all the code from the different sensors, it doesnt give me an output anymore.
the sensors are:
La Crosse tx23 anometer. Connected to pwm 8
AM2302 Humidity sensor, connected to pwm 6
MPL3115A2 Altimeter, connected to the SDA&SCL
HMC588L Magnetometer, also connected to the SDA&SCL
And an 16x2 lcd screen but that is not the issue.

The tx23 takes the North Point from the Magnetometer, that works fine when i test it.
I first tought that it was an i2c issue, but that doesn’t seem to be the problem. Because when i run bits of the code separately they work fine. The humidity, altimeter and magnetometer work fine when combined. And the Tx23 and magnetometer work fine when combined. But when i combine all of them i just dont get any output at my serial monitor.

My code:

#include <DHT.h>
#include <Wire.h>
#include <Adafruit_MPL3115A2.h>
#include <LiquidCrystal.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_HMC5883_U.h>

#define DHTPIN 6
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

Adafruit_MPL3115A2 baro = Adafruit_MPL3115A2();
Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup();
void delay2(unsigned long ms);
void loop();
void magsensor();
void tempLuchtVochtig();
void anometer();
int dataPin = 8;
int bitlenght = -1;
char data[43];
String windrichting;
float luchtdruk;
float temperatuur;
float luchtvochtigheid;
float windsnelheid;
int counter = 1;
float headingDegrees;

void setup() {
Serial.begin(9600);
  lcd.begin(16, 2);
  Serial.println("Test");
  Serial.println(digitalRead(dataPin));
  pinMode(dataPin, INPUT);
  sensor_t sensor;
  mag.getSensor(&sensor);
}
void delay2(unsigned long ms){
	unsigned long start = micros();
	while (micros() - start <= ms);
}
void loop() {
  magsensor();
  delay(500);
  tempLuchtVochtig();
  delay(500);
  anometer();
  delay(500);
  Serial.print("temperatuur: ");
  Serial.println(temperatuur);
  Serial.print("luchtdruk: ");
  Serial.println(luchtdruk);
  Serial.print("HeadingDegrees: ");
  Serial.println(headingDegrees);
  Serial.print("Luchtvochtigheid: ");
  Serial.println(luchtvochtigheid);
  Serial.print("windsnelheid: ");
  Serial.print(windsnelheid);
  Serial.print("Windrichting: ");
  Serial.println(windrichting);
}
void magsensor(){
  sensors_event_t event; 
  mag.getEvent(&event);
  float heading = atan2(event.magnetic.y, event.magnetic.x);
  float declinationAngle = 18.62/1000;
  heading += declinationAngle;
  if(heading < 0)
    heading += 2*PI;
  if(heading > 2*PI)
    heading -= 2*PI;
  headingDegrees = heading * 180/M_PI;
}
void tempLuchtVochtig(){
  luchtdruk = baro.getPressure();
  luchtvochtigheid = dht.readHumidity();
  temperatuur = baro.getTemperature();
}
void anometer(){
  if (bitlenght <0){
      bitlenght = 1230;
   }
  
  pinMode(dataPin, OUTPUT);
  digitalWrite(dataPin, HIGH);
  delay(100);
  
  digitalWrite(dataPin, LOW );
  delay(500);
  
  pinMode(dataPin, INPUT);


  while(digitalRead(dataPin) == LOW){
  }  
  while(digitalRead(dataPin) == HIGH){
  }
  while(digitalRead(dataPin) == LOW){
  }

  for (int i=0 ; i<42 ; i++){

    data[i] = (digitalRead(dataPin) == LOW)? 48 : 49 ;
    delay2(bitlenght);
  }
  String Wind_Dir_PatternStr = "b";
  for (int i =8 ; i> 4 ; i--){
    Wind_Dir_PatternStr += ((data[i] == 48)? 0 : 1);
  }
  int inGraden;
  if (Wind_Dir_PatternStr == "b0000") { inGraden=0;}
  if (Wind_Dir_PatternStr == "b0001") { inGraden=22.5;}
  if (Wind_Dir_PatternStr == "b0010") { inGraden=45;}
  if (Wind_Dir_PatternStr == "b0011") { inGraden=67.5;}
  if (Wind_Dir_PatternStr == "b0100") { inGraden=90;}
  if (Wind_Dir_PatternStr == "b0101") { inGraden=112.5;}
  if (Wind_Dir_PatternStr == "b0110") { inGraden=135;}
  if (Wind_Dir_PatternStr == "b0111") { inGraden=157.5;}
  if (Wind_Dir_PatternStr == "b1000") { inGraden=180;}
  if (Wind_Dir_PatternStr == "b1001") { inGraden=202.5;}
  if (Wind_Dir_PatternStr == "b1010") { inGraden=225;}
  if (Wind_Dir_PatternStr == "b1011") { inGraden=247.5;}
  if (Wind_Dir_PatternStr == "b1100") { inGraden=270;}
  if (Wind_Dir_PatternStr == "b1101") { inGraden=292.5;}
  if (Wind_Dir_PatternStr == "b1110") { inGraden=315;}
  if (Wind_Dir_PatternStr == "b1111") { inGraden=337.5;}
  
  float nieuwePositie = headingDegrees - inGraden;
  windrichting = "";
  
  if (nieuwePositie > 348.76 && nieuwePositie < 360 || 
      nieuwePositie > 0 && nieuwePositie < 11.26 || 
      nieuwePositie < 0 && nieuwePositie > (-11.25) ||
      nieuwePositie > (-360) && nieuwePositie < (-348.75))
      {windrichting = "N";}
  if (nieuwePositie > 11.25 && nieuwePositie < 33.76 ||
      nieuwePositie > (-348.75) && nieuwePositie < (-326.25))
      {windrichting = "NNO";}
  if (nieuwePositie > 33.75 && nieuwePositie < 56.26 ||
      nieuwePositie > (-326.25) && nieuwePositie < (-303.75))
      {windrichting = "NO";}
  if (nieuwePositie > 56.25 && nieuwePositie < 78.75 ||
      nieuwePositie > -303.75 && nieuwePositie < (-281.25))
      {windrichting = "ONO";}
  if (nieuwePositie > 78.75 && nieuwePositie < 101.25 ||
      nieuwePositie > (-281.25) && nieuwePositie < (-258.75))
      {windrichting = "O";}
  if (nieuwePositie > 101.25 && nieuwePositie < 123.75 ||
      nieuwePositie > (-258.75) && nieuwePositie < (-236.25))
      {windrichting = "OZO";}
  if (nieuwePositie > 123.75 && nieuwePositie < 146.25 ||
      nieuwePositie > (-236.25) && nieuwePositie < (-213.75))
      {windrichting = "ZO";}
  if (nieuwePositie > 146.25 && nieuwePositie < 168.75 ||
      nieuwePositie > (-213.75) && nieuwePositie < (-191.25))
      {windrichting = "ZZO";}
  if (nieuwePositie > 168.75 && nieuwePositie < 191.25 ||
      nieuwePositie > (-191.25) && nieuwePositie < (-168.75))
      {windrichting = "Z";}
  if (nieuwePositie > 191.25 && nieuwePositie < 213.75 ||
      nieuwePositie > (-168.75) && nieuwePositie < (-146.25))
      {windrichting = "ZZW";}
  if (nieuwePositie > 213.75 && nieuwePositie < 236.25 ||
      nieuwePositie > (-146.25) && nieuwePositie < (-123.75))
      {windrichting = "ZW";}
  if (nieuwePositie > 236.25 && nieuwePositie < 258.75 ||
      nieuwePositie > (-123.75) && nieuwePositie < (-101.25))
      {windrichting = "WZW";}
  if (nieuwePositie > 258.75 && nieuwePositie < 281.25 ||
      nieuwePositie > (-101.25) && nieuwePositie < (-78.75))
      {windrichting = "W";}
  if (nieuwePositie > 281.25 && nieuwePositie < 303.75 ||
      nieuwePositie > (-78.75) && nieuwePositie < (-56.25))
      {windrichting = "WNW";}
  if (nieuwePositie > 303.75 && nieuwePositie < 326.25 ||
      nieuwePositie > (-56.25) && nieuwePositie < (-33.75))
      {windrichting = "NW";}
  if (nieuwePositie > 326.25 && nieuwePositie < 348.75 ||
      nieuwePositie > (-33.75) && nieuwePositie < (-11.25))
      {windrichting = "NNW";}
 
  int bit17 = 0;
  int bit16 = 0;
  int bit15 = 0;
  int bit14 = 0;
  int bit13 = 0;
  int bit12 = 0;
  int bit11 = 0;
  int bit10 = 0;
  int bit9 = 0;
  
  if(((data[17] == 48)? 0 : 1) == 1){bit17 = 256; }
  if(((data[16] == 48)? 0 : 1) == 1){bit16 = 128; }
  if(((data[15] == 48)? 0 : 1) == 1){bit15 = 64; }
  if(((data[14] == 48)? 0 : 1) == 1){bit14 = 32; }
  if(((data[13] == 48)? 0 : 1) == 1){bit13 = 16; }
  if(((data[12] == 48)? 0 : 1) == 1){bit12 = 8; }
  if(((data[11] == 48)? 0 : 1) == 1){bit11 = 4; }
  if(((data[10] == 48)? 0 : 1) == 1){bit10 = 2; }
  if(((data[9] == 48)? 0 : 1) == 1) {bit9 = 1; }
  
  float tempNum = bit17+bit16+bit15+bit14+bit13+bit12+bit11+bit10+bit9;
  windsnelheid = (tempNum / 10) * 3.6;
}

Hi, welcome to the forum.

Do you get the text "Test" in the serial monitor ? or not even that ? If not, could you print something before lcd.begin ?

Both the I2C sensors may not be connected directly to the SDA and SCL of the Arduino Mega 2560 board. Those sensors are 3.3V and the Arduino Mega has 10k pullup resistors from SDA and SCL to 5V.
You need a I2C level shifter to create a 3.3V I2C bus.

Hi, thank you for your reply.

My I2C sensors are indeed not directly connected too the arduino. But i don't think the problems lays there. When i connect every sensor except the Tx23 anometer, then they work fine. The problem starts when i add the piece of code for the tx23. then i just simply dont get any output anymore.

And i do get the "Test" message in my serial monitor.

After searching a while, I read the many people having trouble with reading the tx23. I did not find a proven good library for the Arduino. And I don't have a tx23 to test it :frowning:
Running out of memory should be no problem, it is a Mega.

The tx23 is running fine on its own, that doesn't mean that the problem is somewhere else. It is possible that there is something with the tx23 code, that barely works on its own, and is slightly out of tune with other sensors connected.

The tx23 uses pin 8, I think that is okay, pin 8 is not used for something else.
Pin 8 is also an output of TIMER4. I think the libraries don't use TIMER4.

The code for the tx23 could stop the sketch in a while-loop.
Where did you find that code for the tx23 ?
Perhaps the code is working, but this is wrong : digitalWrite ( dataPin , HIGH ) ;
The data is a open-collector signal line, the Arduino should let the pullup resistor make the signal high. Forcing a HIGH output could perhaps damage the tx23.

This code is for the Raspberry Pi : https://github.com/ToninoTarsi/swpi/blob/master/TX23/RPi_TX23.c
That code does not make the output high, but releases the data signal, and the pullup resistor will make the signal high. That is how it should be used. But I don't know if that code is working.

Do you have a pullup resistor at pin 8 ? Which value ? a pullup to the 5V ?

Do you use a i2c level shifter for the sensors ? What else beside "Test" is printed to the serial monitor ?

Is it possible that the power pin or the GND for the tx23 is influenced by something ? for example a bad breadboard, or a high current going through the same wire ?

Hi,

I used code i found here: http://forum.arduino.cc/index.php?topic=62753.0
And adjusted it till it worked, with info i found here: La Crosse TX23U Anemometer Communication Protocol – john.geek.nz

I went in this project with a little arduino knowledge and some c# knowledge. So im kinda a newbie to this :o

"Test" gets printed, and then a "1", nothing more. The 1 is coming from " Serial.println(digitalRead(dataPin));"

We connected the wires like there was told in the datasheets we found about the sensors.
The sensors are wired like this:
MPL3115A2 precision Altimeter:
Vin to 5V
GND to GND
SCL to SCL21
SDA to SDA 20

AM2302 Humidity sensor:
pin 1 to ground
pin 2 not connected
pin 3 to pwm 6
pin 4 to 5v
and an 4.7k resistor between the data and 5v

HMC588L Magnetometer:
SCL to SCL
SDA to SDA
GND to GND
Vin to 5V

And the tx23:
brown wire to pwm 8
red wire to 3.3 V
other 2 wires to GND
With an 10k resistor between the dataline and power line.

And to be honest, i have no idea what an "i2c level shifter" is :confused:

A i2c level shifter connects a 3.3V I2C bus to a 5V I2C bus. Google it :wink:

At this moment I have a "i2c halted" feeling. That means that as soon as the i2c is used, the sketch is halted.
I assume that the I2C modules have a 3.3V voltage regulator on the module ? The sensors themself are 3.3V sensors.

You may not connect those I2C sensors directly to the Arduino Mega SDA and SCL.
Perhaps the modules are from Adafruit and have already level shifting on the module ?
Can you give links to the modules that you use ?

Does the tx23 work at 3.3V ? or is 5V needed ?
The tx23 is powered with 3.3V and the dataline is sometimes set to 5V and the pullup to 3.3V. Sorry, that can't be right.

Good job, that you made the tx23 work. Many others have failed as I understand.
Do you understand the difference between setting an output HIGH (a strong 5V output) or release the line by making it input and let the resistor pull the signal high ?
Could you make a test sketch for the tx23 with releasing the signal. Use only the tx23 and disconnect everything else from the board.

At this moment I can not pinpoint the problem. So that is why I want to fix all the little things and hope that one of them was the cause. That is the way things often go.

It are indeed Adafruit sensors.
They have shifting if i read correctly:
Altimeter> https://www.adafruit.com/products/1893
Magnetometer> https://www.adafruit.com/products/1746
Humidity> http://www.adafruit.com/products/385

I tested the Tx23 on 5V and that works fine too.

If i understand correctly, setting the output high will give the Tx23 a signal that you want data from it. then you pull it low for 500ms to let it calculate his measurements. Then you set the pin as an input so that the arduino can recieve data from it.

I tested it with the sketch below. And when i comment the line where the signal gets pulled High. Then the code still works, so its not necessary.

void setup();
void delay2(unsigned long ms);
void loop();
int collectdata(void);
void showdatasimple();
int dataPin = 8;
int bitlenght = -1;
char data[43];
String Wind_Direction;

void setup() {
  Serial.begin(9600);
  Serial.print("Tx23 Wind speed and direction Test");
  Serial.println(digitalRead(dataPin));
  pinMode(dataPin, INPUT);
}

void delay2(unsigned long ms){
	unsigned long start = micros();
	while (micros() - start <= ms);
}

void loop() {
  collectdata();
}
int collectdata(void){
  delay(1500);
   if (bitlenght <0){
      bitlenght = 1230;
   }
  
  pinMode(dataPin, OUTPUT);
  //mdigitalWrite(dataPin, HIGH);
  delay(100);
  
  digitalWrite(dataPin, LOW );
  delay(500);
  
  pinMode(dataPin, INPUT);

  while(digitalRead(dataPin) == LOW){
  }  
  while(digitalRead(dataPin) == HIGH){
  }
  while(digitalRead(dataPin) == LOW){
  }
  for (int i=0 ; i<42 ; i++){
    data[i] = (digitalRead(dataPin) == LOW)? 48 : 49 ;
    delay2(bitlenght);
  }
  showdatasimple();
  return 0;
}
void showdatasimple(){
  Serial.println("");
  String Wind_Dir_PatternStr = "b";
  String Wind_Dir_DescStr = "";
  for (int i =8 ; i> 4 ; i--){
    Wind_Dir_PatternStr += ((data[i] == 48)? 0 : 1);
  }
 if (Wind_Dir_PatternStr == "b0000") { Wind_Dir_DescStr="N";}
  if (Wind_Dir_PatternStr == "b0001") { Wind_Dir_DescStr="NNO";}
  if (Wind_Dir_PatternStr == "b0010") { Wind_Dir_DescStr="NO";}
  if (Wind_Dir_PatternStr == "b0011") { Wind_Dir_DescStr="ONO";}
  if (Wind_Dir_PatternStr == "b0100") { Wind_Dir_DescStr="O";}
  if (Wind_Dir_PatternStr == "b0101") { Wind_Dir_DescStr="OZO";}
  if (Wind_Dir_PatternStr == "b0110") { Wind_Dir_DescStr="ZO";}
  if (Wind_Dir_PatternStr == "b0111") { Wind_Dir_DescStr="ZZO";}
  if (Wind_Dir_PatternStr == "b1000") { Wind_Dir_DescStr="Z";}
  if (Wind_Dir_PatternStr == "b1001") { Wind_Dir_DescStr="ZZW";}
  if (Wind_Dir_PatternStr == "b1010") { Wind_Dir_DescStr="ZW";}
  if (Wind_Dir_PatternStr == "b1011") { Wind_Dir_DescStr="WZW";}
  if (Wind_Dir_PatternStr == "b1100") { Wind_Dir_DescStr="W";}
  if (Wind_Dir_PatternStr == "b1101") { Wind_Dir_DescStr="WNW";}
  if (Wind_Dir_PatternStr == "b1110") { Wind_Dir_DescStr="NW";}
  if (Wind_Dir_PatternStr == "b1111") { Wind_Dir_DescStr="NNW";}
  Serial.println(Wind_Dir_DescStr);
  for (int i =20 ; i> 8 ; i--){
    Serial.print( ((data[i] == 48)? 0 : 1) );
  }
  
  int bit17 = 0;
  int bit16 = 0;
  int bit15 = 0;
  int bit14 = 0;
  int bit13 = 0;
  int bit12 = 0;
  int bit11 = 0;
  int bit10 = 0;
  int bit9 = 0;
  
  Serial.print(" ");
  if(((data[17] == 48)? 0 : 1) == 1){bit17 = 256; }
  if(((data[16] == 48)? 0 : 1) == 1){bit16 = 128; }
  if(((data[15] == 48)? 0 : 1) == 1){bit15 = 64; }
  if(((data[14] == 48)? 0 : 1) == 1){bit14 = 32; }
  if(((data[13] == 48)? 0 : 1) == 1){bit13 = 16; }
  if(((data[12] == 48)? 0 : 1) == 1){bit12 = 8; }
  if(((data[11] == 48)? 0 : 1) == 1){bit11 = 4; }
  if(((data[10] == 48)? 0 : 1) == 1){bit10 = 2; }
  if(((data[9] == 48)? 0 : 1) == 1) {bit9 = 1; }
  
  float tempNum = bit17+bit16+bit15+bit14+bit13+bit12+bit11+bit10+bit9;
  float windsnelheid = (tempNum / 10) * 3.6;
  Serial.print(windsnelheid);
}

Very good. The i2c modules have a voltage regulator and i2c level shifting on the module. That's no problem.

I read more about the tx23 module. Is that powered with 3V from two batteries ? Or from the Arduino 5V or 3.3V ?
It depends on the output of the t23 modules, but 3V from batteries is not enough to detect a HIGH for an Arduino.
Do you have a multimeter ? Suppose the tx23 will be powered with 3.3V and a pullup resistor of 10k is to 3.3V, will the signal voltage be high enough ?

I doubt if that code is correct. When a pin is set as OUTPUT, the output becomes LOW as default, but perhaps it can be HIGH. I would have to check the libraries, I think this was changed a while ago.

// Make pin LOW
pinMode( dataPin, INPUT);
digitalWrite( dataPin, LOW);

// Release the pin (let the external pullup resistor pull it high)
pinMode( dataPin, INPUT);

// Release the pin (enable the internal pullup resistor)
pinMode( dataPin, INPUT_PULLUP);

I'm sorry, but I still don't have a clue. I did install the libraries and compiled it, but it was fine.
Perhaps you can print more messages after every function call. I would like to know if the sketch halts as soon as i2c is used.
Perhaps you could try another pin for the tx23, the pin might be damaged.
Perhaps you use a breadboard with bad contacts and have the same ground for the sensors and the tx23 ?
Could you make a photo of your project ?

Hi,

How can i check if the sketch halts as soon as i2c is used?
I did try my code by disableng every sensor until it worked.
I get 0 prints when i enable them all.
When i disable the magnetometer(compass), i get readings and prints from the humidity sensor and the Tx23. And i get an print from the altimeter. But the altimeter reads nothing, it only prints the highest measurable value. (An air pressure of 67108864 millibar doesn't seems legit)
When i disable the altimeter, but not the magnetometer. Then i get 0 prints again.
When i disable both the altimeter and the magnetometer, then the rest works fine.
When i disable the Humidity sensor i get 0 prints.
Same when i disable tx23.

I do use the same GND for every sensor, all the sensor are connected to a circuit board, and the board to the ardiuno.

Picture:

Thanks for the photo. I can't follow all the wires, but so far it seems okay. You could do an extra check on the ground wire, test if every ground is soldered well at the bottom.

Also thanks for all the testing, but I can't see what could be the cause.

The Arduino Mega board is only powerd with the USB cable ? Could you measure the 5V pin ? The Arduino Mega needs 4.5V or higher.

More serial output could be like this:

  Serial.println("creating sensor");
  sensor_t sensor;
  Serial.println("Calling mag.getSensor");
  mag.getSensor(&sensor);
  Serial.println("end of setup");

If the I2C is wrong, the sketch could halt in the mag.getSensor() function.

Are you sure that the sensor modules are powered with 5V to Vin ? That is needed to make the level shifting work.

Thank you for your help so far.

Its not the mag.getSensor() funcion.
this was my serial print:
Test
1
creating sensor
Calling mag.getSensor
end of setup

The board is powered ony by USB, i measured the 5V pin, it had 4.99V on it so that isn’t the problem i think.

Im 100% sure that the sensors are powered with 5V. I used the sketch below to test the humidity, altimeter and mangetometer. I assume that the level shifting works too in this sketch. This is kinda the same as my begin sketch. But this one i used to show the variables on a lcd screen. I didnt change any wires too make this one work. so im pretty sure that it isn’t an cable problem.

#include <DHT.h>
#include <Wire.h>
#include <Adafruit_MPL3115A2.h>
#include <LiquidCrystal.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_HMC5883_U.h>


#define DHTPIN 6
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

Adafruit_MPL3115A2 baro = Adafruit_MPL3115A2();
Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);


int counter = 1;

void setup() {
  Serial.begin(9600);
  lcd.begin(16, 2);
   sensor_t sensor;
  mag.getSensor(&sensor);
}

void loop() {

  sensors_event_t event; 
  mag.getEvent(&event);
  float luchtdruk = baro.getPressure();
  float luchtvochtigheid = dht.readHumidity();
  float temperatuur = baro.getTemperature();
  
  float heading = atan2(event.magnetic.y, event.magnetic.x);
  float declinationAngle = 18.62/1000;
  heading += declinationAngle;
  if(heading < 0)
    heading += 2*PI;
  if(heading > 2*PI)
    heading -= 2*PI;
  float headingDegrees = heading * 180/M_PI; 
  
  delay(1000);
  lcd.clear();
  
  lcd.setCursor(0, 0);
  lcd.print("Millibar: ");
  lcd.print(luchtdruk/100);
  Serial.print("Millibar: ");
  Serial.println(luchtdruk/100);

  
  lcd.setCursor(0, 1);
  lcd.print("Graden: ");
  lcd.print(temperatuur);
  Serial.print("Graden: ");
  Serial.println(temperatuur);
  
  
  Serial.print("Luchtvochtigheid: ");
  Serial.println(luchtvochtigheid);
  
  
  Serial.print("Heading: ");
  Serial.println(headingDegrees);
  Serial.print("Meting: ");
  Serial.println(counter);
  Serial.println(" ");
  
  delay(2500);
  
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("LvH: ");
  lcd.print(luchtvochtigheid);
  lcd.print("%");

  lcd.setCursor(0, 1);
  lcd.print("Heading: ");
  lcd.print(headingDegrees);
  // 180000
  
  delay(2500);
  counter = counter + 1;
}

Okay. I think we can assume that all sensors and hardware and voltages and level shifting is okay.
The Tx23 is working on its own but not together with the rest.
Well, that was your question in the first place, and we didn’t get closer to the problem :frowning:

This is really a tough one. I’m terribly sorry, but I don’t know what to do.

There are a bunch of minor things that can be done.
Did you try another pin ? You could measure the voltages of the Tx23 and add more Serial.println() function. Maybe 20 or 50 more. For example in the anometer() function. Because there are while loops that could run forever. Maybe there is someone else that can take a look at it. I could rewrite a part of your sketch to see if that would make a difference.
In the Netherlands is in the forum of tweakers.net a section for Arduino. There are many people that are smarter than me. If you post a message there, you can add a link to this thread, so they know what you tried.

Thanks for your help :slight_smile:

Im gonna give it a try on tweakers.net. Setting the Tx23 on another pin didn't solve it. Im gonna try your tip with the Serial.println() function, to see where it stops doing what its supposed to do.

For anyone interested. I managed to fix my problem by using a different library for my magnetometer. now everything is working perfectly fine :D. The code apparently kept getting stuck on creating the magnetic.event

here is the working code:

#include <Wire.h>
#include <HMC5883L.h>
#include <DHT.h>
#include <Adafruit_MPL3115A2.h>
#include <LiquidCrystal.h>
#include <Adafruit_Sensor.h>

#define DHTPIN 6
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

Adafruit_MPL3115A2 baro = Adafruit_MPL3115A2();
LiquidCrystal lcd(12, 11, 5, 4, 3, 7);

int dataPin = 8;
int bitlenght = -1;
char data[43];
String windrichting;
float luchtdruk;
float temperatuur;
float luchtvochtigheid;
float windsnelheid;
int counter = 1;
float headingDegrees;

HMC5883L compass;
void setup() {
Serial.begin(9600);

  Wire.begin();
  
  lcd.begin(16, 2);
  
  Serial.println("Test");
  
  Serial.println(digitalRead(dataPin));
  pinMode(dataPin, INPUT);
  compass = HMC5883L();
  int error; 
  error = compass.SetScale(1.3);
  if(error != 0) Serial.println(compass.GetErrorText(error));
  error = compass.SetMeasurementMode(Measurement_Continuous);
  if(error != 0) Serial.println(compass.GetErrorText(error));
}
void delay2(unsigned long ms){
	unsigned long start = micros();
	while (micros() - start <= ms);
}
void loop() {
  headingDegrees = getHeading();
  delay(2500);
  Luchtdruk();
  delay(2500);
  luchtvochtigTemp();
  delay(2500);
  anometer();
  Serial.print("Luchtvochtigheid: ");
  Serial.println(luchtvochtigheid);
  Serial.print("Temperatuur: ");
  Serial.println(temperatuur);
  Serial.print("luchtdruk: ");
  Serial.println(luchtdruk);
  Serial.print("HeadingDegrees: ");
  Serial.println(headingDegrees);
  Serial.print("windsnelheid: ");
  Serial.println(windsnelheid);
  Serial.print("Windrichting: ");
  Serial.println(windrichting);
  Serial.println(" ");
  delay(2500);
}
float getHeading(){
 MagnetometerScaled scaled = compass.ReadScaledAxis(); //scaled values from compass.
 float heading = atan2(scaled.YAxis, scaled.XAxis);
float declinationAngle = 18.62/1000;
  heading += declinationAngle;
  if(heading <0)
   heading += 2*PI;
  if(heading > 2*PI)
   heading -= 2*PI;

 return heading * RAD_TO_DEG; //radians to degrees
}
void Luchtdruk(){
  float tempDruk = baro.getPressure();
  luchtdruk = tempDruk / 100;
  float temporary = baro.getTemperature();

}
void luchtvochtigTemp(){
    luchtvochtigheid = dht.readHumidity();
    temperatuur = dht.readTemperature();
}
void anometer(){
  if (bitlenght <0){
      bitlenght = 1230;
   }
  
  pinMode( dataPin, OUTPUT);
  digitalWrite( dataPin, LOW);
  delay(500);
  
  pinMode(dataPin, INPUT);
  delay(50);
  pinMode( dataPin, INPUT_PULLUP);


  while(digitalRead(dataPin) == LOW){
  }  
  while(digitalRead(dataPin) == HIGH){
  }
  while(digitalRead(dataPin) == LOW){
  }

  for (int i=0 ; i<42 ; i++){

    data[i] = (digitalRead(dataPin) == LOW)? 48 : 49 ;
    delay2(bitlenght);
  }
  String Wind_Dir_PatternStr = "b";
  for (int i =8 ; i> 4 ; i--){
    Wind_Dir_PatternStr += ((data[i] == 48)? 0 : 1);
  }
  int inGraden;
  if (Wind_Dir_PatternStr == "b0000") { inGraden=0;}
  if (Wind_Dir_PatternStr == "b0001") { inGraden=22.5;}
  if (Wind_Dir_PatternStr == "b0010") { inGraden=45;}
  if (Wind_Dir_PatternStr == "b0011") { inGraden=67.5;}
  if (Wind_Dir_PatternStr == "b0100") { inGraden=90;}
  if (Wind_Dir_PatternStr == "b0101") { inGraden=112.5;}
  if (Wind_Dir_PatternStr == "b0110") { inGraden=135;}
  if (Wind_Dir_PatternStr == "b0111") { inGraden=157.5;}
  if (Wind_Dir_PatternStr == "b1000") { inGraden=180;}
  if (Wind_Dir_PatternStr == "b1001") { inGraden=202.5;}
  if (Wind_Dir_PatternStr == "b1010") { inGraden=225;}
  if (Wind_Dir_PatternStr == "b1011") { inGraden=247.5;}
  if (Wind_Dir_PatternStr == "b1100") { inGraden=270;}
  if (Wind_Dir_PatternStr == "b1101") { inGraden=292.5;}
  if (Wind_Dir_PatternStr == "b1110") { inGraden=315;}
  if (Wind_Dir_PatternStr == "b1111") { inGraden=337.5;}
  
  float nieuwePositie = headingDegrees - inGraden;
  windrichting = "";
  
  if (nieuwePositie > 348.76 && nieuwePositie < 360 || 
      nieuwePositie > 0 && nieuwePositie < 11.26 || 
      nieuwePositie < 0 && nieuwePositie > (-11.25) ||
      nieuwePositie > (-360) && nieuwePositie < (-348.75))
      {windrichting = "N";}
  if (nieuwePositie > 11.25 && nieuwePositie < 33.76 ||
      nieuwePositie > (-348.75) && nieuwePositie < (-326.25))
      {windrichting = "NNO";}
  if (nieuwePositie > 33.75 && nieuwePositie < 56.26 ||
      nieuwePositie > (-326.25) && nieuwePositie < (-303.75))
      {windrichting = "NO";}
  if (nieuwePositie > 56.25 && nieuwePositie < 78.75 ||
      nieuwePositie > -303.75 && nieuwePositie < (-281.25))
      {windrichting = "ONO";}
  if (nieuwePositie > 78.75 && nieuwePositie < 101.25 ||
      nieuwePositie > (-281.25) && nieuwePositie < (-258.75))
      {windrichting = "O";}
  if (nieuwePositie > 101.25 && nieuwePositie < 123.75 ||
      nieuwePositie > (-258.75) && nieuwePositie < (-236.25))
      {windrichting = "OZO";}
  if (nieuwePositie > 123.75 && nieuwePositie < 146.25 ||
      nieuwePositie > (-236.25) && nieuwePositie < (-213.75))
      {windrichting = "ZO";}
  if (nieuwePositie > 146.25 && nieuwePositie < 168.75 ||
      nieuwePositie > (-213.75) && nieuwePositie < (-191.25))
      {windrichting = "ZZO";}
  if (nieuwePositie > 168.75 && nieuwePositie < 191.25 ||
      nieuwePositie > (-191.25) && nieuwePositie < (-168.75))
      {windrichting = "Z";}
  if (nieuwePositie > 191.25 && nieuwePositie < 213.75 ||
      nieuwePositie > (-168.75) && nieuwePositie < (-146.25))
      {windrichting = "ZZW";}
  if (nieuwePositie > 213.75 && nieuwePositie < 236.25 ||
      nieuwePositie > (-146.25) && nieuwePositie < (-123.75))
      {windrichting = "ZW";}
  if (nieuwePositie > 236.25 && nieuwePositie < 258.75 ||
      nieuwePositie > (-123.75) && nieuwePositie < (-101.25))
      {windrichting = "WZW";}
  if (nieuwePositie > 258.75 && nieuwePositie < 281.25 ||
      nieuwePositie > (-101.25) && nieuwePositie < (-78.75))
      {windrichting = "W";}
  if (nieuwePositie > 281.25 && nieuwePositie < 303.75 ||
      nieuwePositie > (-78.75) && nieuwePositie < (-56.25))
      {windrichting = "WNW";}
  if (nieuwePositie > 303.75 && nieuwePositie < 326.25 ||
      nieuwePositie > (-56.25) && nieuwePositie < (-33.75))
      {windrichting = "NW";}
  if (nieuwePositie > 326.25 && nieuwePositie < 348.75 ||
      nieuwePositie > (-33.75) && nieuwePositie < (-11.25))
      {windrichting = "NNW";}
 
  int bit17 = 0;
  int bit16 = 0;
  int bit15 = 0;
  int bit14 = 0;
  int bit13 = 0;
  int bit12 = 0;
  int bit11 = 0;
  int bit10 = 0;
  int bit9 = 0;
  
  if(((data[17] == 48)? 0 : 1) == 1){bit17 = 256; }
  if(((data[16] == 48)? 0 : 1) == 1){bit16 = 128; }
  if(((data[15] == 48)? 0 : 1) == 1){bit15 = 64; }
  if(((data[14] == 48)? 0 : 1) == 1){bit14 = 32; }
  if(((data[13] == 48)? 0 : 1) == 1){bit13 = 16; }
  if(((data[12] == 48)? 0 : 1) == 1){bit12 = 8; }
  if(((data[11] == 48)? 0 : 1) == 1){bit11 = 4; }
  if(((data[10] == 48)? 0 : 1) == 1){bit10 = 2; }
  if(((data[9] == 48)? 0 : 1) == 1) {bit9 = 1; }
  
  float tempNum = bit17+bit16+bit15+bit14+bit13+bit12+bit11+bit10+bit9;
  windsnelheid = (tempNum / 10) * 3.6;
}

I’m glad it is working :smiley:
But I don’t understand what was wrong with the previous library :confused: