LCD screen not working when ethenet code added.

Hi all, I am very new to arduino, if possible I could do with some help fixing my code. I have made a Geiger counter/EMF sensor which works fine.

But as soon as I tried to integrate code for an Ethernet shield the LCD screens fail to display the information from the sensors.

But the Ethernet shield connects to the internet and sends the data fine.

so I’m stuck any idea why the LCD’s have stopped displaying the sensor readings?

// include the library code:
#include <LiquidCrystal.h>
#include <Ethernet.h>
#include <SPI.h>
#include "plotly_streaming_ethernet.h"

 // initialize the library with the numbers of the interface pins
 LiquidCrystal lcd (8, 2, 5, 6, 3, 7);
 LiquidCrystal lcd2(8, 9, 5, 6, 3, 7); 
 

int probePin = A0; // analog AO
int sensorValue = 0; // reading from probePin

// Conversion factor - CPM to uSV/h
#define CONV_FACTOR 0.0057

// Variables
int geiger_input = A1;
long count = 0;
long countPerMinute = 0;
long timePrevious = 0;
long timePreviousMeassure = 0;
long time = 0;
long countPrevious = 0;
float radiationValue = 0.0;

#define nTraces 2

char *tokens[nTraces] = {"zecyt0o7jk", "2jz5j19n8b"};

plotly graph = plotly("extravodka", "xnvv088e13", tokens, "environmental project", nTraces);

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte my_ip[] = { 199, 168, 222, 18 }; 

void startEthernet(){
    Serial.println("... Initializing ethernet");
    if(Ethernet.begin(mac) == 0){
        Serial.println("... Failed to configure Ethernet using DHCP");
       
        Ethernet.begin(mac, my_ip);
    }
    Serial.println("... Done initializing ethernet");
    delay(1000);
}
void setup(){
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  startEthernet();

  graph.fileopt="overwrite"; 
  bool success;
  success = graph.init();
  if(!success){while(true){}}
  graph.openStream();
  
  
  pinMode(geiger_input, INPUT);
  digitalWrite(geiger_input,HIGH);
  for (int i=0;i<5;i++)


  //set up the LCD\'s number of columns and rows:
  lcd.begin(16, 2);
  lcd2.begin(16, 2);
  
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Radiation Sensor");
  
  delay(1000);

  lcd.clear();  
  lcd.setCursor(0, 0);
  lcd.print("CPM=");
  lcd.setCursor(4,0);
  lcd.print(6*count);
  lcd.setCursor(0,1);
  lcd.print(radiationValue);
  
 attachInterrupt(0,countPulse,FALLING);
}

unsigned long x;
int y;

void loop(){
   
  graph.plot(millis(), analogRead(A0), tokens[0]);
  graph.plot(millis(), (radiationValue), tokens[1]);

  delay(50);

  if (millis()-timePreviousMeassure > 10000){
    
    countPerMinute = 6*count;
    radiationValue = countPerMinute * CONV_FACTOR;
    timePreviousMeassure = millis();
    lcd.clear();    
    lcd.setCursor(0, 0);
    lcd.print("CPM=");
    lcd.setCursor(4,0);
    lcd.print(countPerMinute);
    lcd.setCursor(0,1);
    lcd.print(radiationValue,4);
    lcd.setCursor(6,1);
    lcd.print(" uSv/h");
    count = 0;
  }
   {
   sensorValue = analogRead(probePin);  // take a reading from the probe
   
      lcd2.setCursor(0, 0);
      lcd2.print("EMF=");
      lcd2.setCursor(5, 0);
      lcd2.print(sensorValue);   // Show a 0
      lcd2.setCursor(0, 1);
      lcd2.print("0=Low - 600=High");
     
    
    delay(3000);
    lcd2.clear();
   
   }
  }

 void countPulse(){
 

  detachInterrupt(0);
  count++;
  while(digitalRead(A1)==0){
  
  
  attachInterrupt(0,countPulse,FALLING);
  }
 }

Sorry but this just leaps out of the page at me.

while(digitalRead(A1)==0){
  attachInterrupt(0,countPulse,FALLING);
  }

How many times do you need to attach an interrupt?

My understanding is that it makes up part of the Geiger counter calculation. But like I said I'm very new to this so I could be wrong but the Geiger counter works fine and dose read radiation accurately in a lab environment. It's just the lcd screens I can not get to work correctly.

Sorry if I'm missing your point I am currently naive when it comes to arduino but am trying to learn as fast as possible.

I see it is duplicated in the void setup and loop, I have removed the interrupt from the loop and now the lcd is just showing an x and the lcd2 is still blank.

Can you post the working Geiger counter code separately.

I suspect that all that is needed in the function countPulse() is

void countPulse(){
  count++;
}

This line

long count = 0;

should be

volatile long count = 0;

so that the compiler knows the value can be changed by the interrupt.

You have these two separate lines

countPerMinute = 6*count;


count = 0;

It would be wise to put them one after the other while temporarily halting interrupts. Just save the value of count to another variable, rather than do calculations with it directly. Something like this

noInterrupts();
long countValue = count;
count = 0;
interrupts();

The reason for halting the interrupts is because you have to read 4 bytes and you don't want an interrupt to occur in the middle.

Also you should be aware that all the LCD stuff that is presently between those 2 lines takes a very long time.

...R

Hi Robin, below is the Geiger counter code on its own it works with an LCD screen.

// include the library code:
#include <LiquidCrystal.h>

 // initialize the library with the numbers of the interface pins
 LiquidCrystal lcd(12, 11, 5, 4, 3, 7);




// Conversion factor - CPM to uSV/h
#define CONV_FACTOR 0.0057

// Variables
int geiger_input = 2;
long count = 0;
long countPerMinute = 0;
long timePrevious = 0;
long timePreviousMeassure = 0;
long time = 0;
long countPrevious = 0;
float radiationValue = 0.0;

void setup(){
  pinMode(geiger_input, INPUT);
  digitalWrite(geiger_input,HIGH);
  for (int i=0;i<5;i++){
      }

  Serial.begin(9600);

  //set up the LCD\'s number of columns and rows:
  lcd.begin(16, 2);
  
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Radiation Sensor");
  
  delay(1000);

  lcd.clear();  
  lcd.setCursor(0, 0);
  lcd.print("CPM=");
  lcd.setCursor(4,0);
  lcd.print(6*count);
  lcd.setCursor(0,1);
  lcd.print(radiationValue);

  attachInterrupt(0,countPulse,FALLING);

}

void loop(){
  if (millis()-timePreviousMeassure > 10000){
    countPerMinute = 6*count;
    radiationValue = countPerMinute * CONV_FACTOR;
    timePreviousMeassure = millis();
    Serial.print("cpm = "); 
    Serial.print(countPerMinute,DEC);
    Serial.print(" - ");
    Serial.print("uSv/h = ");
    Serial.println(radiationValue,4);      
    lcd.clear();    
    lcd.setCursor(0, 0);
    lcd.print("CPM=");
    lcd.setCursor(4,0);
    lcd.print(countPerMinute);
    lcd.setCursor(0,1);
    lcd.print(radiationValue,4);
    lcd.setCursor(6,1);
    lcd.print(" uSv/h");
    count = 0;

  }

}

void countPulse(){
  detachInterrupt(0);
  count++;
  while(digitalRead(2)==0){
  }
  attachInterrupt(0,countPulse,FALLING);
}

I did think that the enable pin was being used by the Ethernet shield as well as the LCD so I did check the Ethernet pins and changed accordingly but the screens still fail to work. Just for reference my project is using a uno.

In the code that you have working, you initialise your LCD with

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

Yet in the code that doesn't work you have

 LiquidCrystal lcd2(8, 9, 5, 6, 3, 7);

I assume you realised that pins 12 and 11 conflict with your ethernet shield. But did you physically change them or just modify your sketch?

Yes I did realize, and I have changed the sketch as well as the real world wiring.

:slight_smile:

So does the working code (you gave in answer #6) work with your modified LCD parameters? And which LCD (I notice you have 2! )

Oh and BTW. I recall a similar post regarding a conflict with an ethernet shield. It turned out that it wasn't the pins used for ethernet causing the problem at all. It was one of the pins used to access the SDcard reader (included on the ethernet shield). Does your ethernet shield have such an extra?

yes my Ethernet card dose have an sd card option, i have tried the working code from post 6 with the modified LCD pin code and it will work fine for one LCD but not the other.

LiquidCrystal lcd2(8, 9, 5, 6, 3, 7);

works fine.

LiquidCrystal lcd2(2, 9, 5, 6, 3, 7);

dose not work.

mmmmm now Im even more stumped as the code in post 1 will work fine if the Ethernet code is removed and that uses the both LCD codes.

think i should go back and check the sd card pins.

sorry in the LCD code i meant 9 swapped for 2 not 8. :confused:

Kinetic: sorry in the LCD code i meant 9 swapped for 2 not 8. :confused:

S'OK I knew what you meant :)

I have just had a new look over the Ethernet pin layout and what it uses as well as the SD pins. what I have found is that the Ethernet shield uses pin 2 so i have moved the LCD to pin 1 not sure if I’m allowed to do that but that’s what i have done :confused: . I have also moved my sensors from A0,A1 that are the SD card pins for the Ethernet shield and put them on A4,A5

below is the current code I am working with.

// include the library code:
#include <LiquidCrystal.h>
#include <Ethernet.h>
#include <SPI.h>
#include "plotly_streaming_ethernet.h"

 // initialize the library with the numbers of the interface pins
 LiquidCrystal lcd (8, 1, 5, 6, 3, 7);
 LiquidCrystal lcd2(8, 9, 5, 6, 3, 7); 
 

int probePin = A4; // analog A4
int sensorValue = 0; // reading from probePin

// Conversion factor - CPM to uSV/h
#define CONV_FACTOR 0.0057

// Variables
int geiger_input = A5;
volatile long count = 0;
long countPerMinute = 0;
long timePrevious = 0;
long timePreviousMeassure = 0;
long time = 0;
long countPrevious = 0;
float radiationValue = 0.0;

#define nTraces 2

char *tokens[nTraces] = {"zecyt0o7jk", "2jz5j19n8b"};

plotly graph = plotly("extravodka", "xnvv088e13", tokens, "environmental project", nTraces);

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte my_ip[] = { 199, 168, 222, 18 }; 

void startEthernet(){
    Serial.println("... Initializing ethernet");
    if(Ethernet.begin(mac) == 0){
        Serial.println("... Failed to configure Ethernet using DHCP");
       
        Ethernet.begin(mac, my_ip);
    }
    Serial.println("... Done initializing ethernet");
    delay(1000);
}
void setup(){
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  startEthernet();

  graph.fileopt="overwrite"; 
  bool success;
  success = graph.init();
  if(!success){while(true){}}
  graph.openStream();
  
  
  pinMode(geiger_input, INPUT);
  digitalWrite(geiger_input,HIGH);
  for (int i=0;i<5;i++)


  //set up the LCD\'s number of columns and rows:
  lcd.begin(16, 2);
  lcd2.begin(16, 2);
  
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Radiation Sensor");
  
  delay(1000);

  lcd.clear();  
  lcd.setCursor(0, 0);
  lcd.print("CPM=");
  lcd.setCursor(4,0);
  lcd.print(6*count);
  lcd.setCursor(0,1);
  lcd.print(radiationValue);
  
 attachInterrupt(0,countPulse,FALLING);
}

unsigned long x;
int y;

void loop(){
   
  graph.plot(millis(), analogRead(A4), tokens[0]);
  graph.plot(millis(), (radiationValue), tokens[1]);

  delay(50);

  if (millis()-timePreviousMeassure > 10000){
    
    noInterrupts();
    countPerMinute = 6*count;
    count = 0;
    radiationValue = countPerMinute * CONV_FACTOR;
    timePreviousMeassure = millis();
    interrupts();
    
    lcd.clear();    
    lcd.setCursor(0, 0);
    lcd.print("CPM=");
    lcd.setCursor(4,0);
    lcd.print(countPerMinute);
    lcd.setCursor(0,1);
    lcd.print(radiationValue,4);
    lcd.setCursor(6,1);
    lcd.print(" uSv/h");
    
  }
   {
   sensorValue = analogRead(probePin);  // take a reading from the probe
   
      lcd2.setCursor(0, 0);
      lcd2.print("EMF=");
      lcd2.setCursor(5, 0);
      lcd2.print(sensorValue);   // Show a 0
      lcd2.setCursor(0, 1);
      lcd2.print("0=Low - 600=High");
     
    
    delay(3000);
    lcd2.clear();
   
   }
  }

 void countPulse(){
   count++;
 }

Kinetic: So i have moved the LCD to pin 1 not sure if I'm allowed to do that.

Yes you are. As long as you don't try to use Serial, it shouldn't be a problem.

But the important thing is, DOES IT WORK?

well kind of :( I have noticed that the lcd2 that shows the readings from the EMF sensor works now but only once the Ethernet shield has made a connection to the internet, i can live with that outcome. but the LCD/LCD1 is still failing to show any thing let allow the Geiger data.

so I'm still stuck :-[

Kinetic: well kind of :( I have noticed that the lcd2 that shows the readings from the EMF sensor works now but only once the Ethernet shield has made a connection to the internet, i can live with that outcome. but the LCD/LCD1 is still failing to show any thing let allow the Geiger data.

so I'm still stuck :-[

Ok I noticed that you're including plotly_streaming_ethernet.h A quick google and I came up with This I can't help but notice that there are quite mentions of Serial in there. Looks like you CAN'T use pin 1 after all. Time for another pin shuffle :)

mmmm that may be hard as the only other pin left is 2 unless i can use the Analog pins for the LCD but i very much doubt that.

Kinetic: mmmm that may be hard as the only other pin left is 2 unless i can use the Analog pins for the LCD but i very much doubt that.

No reason you can't. ALL pins can be used as digital pins. Which arduino are you using? On an UNO pin A0 can be used as digital pin 14 A1 = PIN 15 A2 = PIN 16 A3 = PIN 17 A4 = PIN 18 A5 = PIN 19

Take your pick and use accordingly.

I have altered the pins for the LCD so now pin one is free for the serial. i had a look at the plotly code that you found unfortunately it uses the library Arduino.h i believe this library is for advance math and will only work on a mega. :frowning: i have started from scratch with the code iv put it below. now LCD2 has no display and lcd1 is stuck on the LCD.print CPM part.

I think I’m going to attend the arduino conference in London if I cant find a fix soon maybe they may have some answers :’(

#include <Dhcp.h>
#include <Dns.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <EthernetUdp.h>
#include <util.h>
#include <SPI.h>
#include <plotly_streaming_ethernet.h>
// include the library code:
#include <LiquidCrystal.h>

// Sign up to plotly here: https://plot.ly
// View your API key and streamtokens here: https://plot.ly/settings
#define nTraces 2
// View your tokens here: https://plot.ly/settings
// Supply as many tokens as data traces
// e.g. if you want to ploty A0 and A1 vs time, supply two tokens
char *tokens[nTraces] = {"zecyt0o7jk", "2jz5j19n8b"};
// arguments: username, api key, streaming token, filename
plotly graph = plotly("extravodka", "xnvv088e13", tokens, "environmental project", nTraces);
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte my_ip[] = { 199, 168, 222, 18 }; // google will tell you: "public ip address"
void startEthernet(){
Serial.println("... Initializing ethernet");
if(Ethernet.begin(mac) == 0){
Serial.println("... Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, my_ip);
}
Serial.println("... Done initializing ethernet");
delay(1000);
}


 // initialize the library with the numbers of the interface pins
 LiquidCrystal lcd (8, 16, 5, 6, 3, 7);
 LiquidCrystal lcd2(8, 17, 5, 6, 3, 7); 
 

int probePin = A4; // analog A4
int sensorValue = 0; // reading from probePin

// Conversion factor - CPM to uSV/h
#define CONV_FACTOR 0.0057

// Variables
int geiger_input = A5;
long count = 0;
long countPerMinute = 0;
long timePrevious = 0;
long timePreviousMeassure = 0;
long time = 0;
long countPrevious = 0;
float radiationValue = 0.0;



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

  
  pinMode(geiger_input, INPUT);
  digitalWrite(geiger_input,HIGH);
  for (int i=0;i<5;i++){
}
  //set up the LCD\'s number of columns and rows:
  lcd.begin(16, 2);
  lcd2.begin(16, 2);
  
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Radiation Sensor");
  
  delay(1000);

  lcd.clear();  
  lcd.setCursor(0, 0);
  lcd.print("CPM=");
  lcd.setCursor(4,0);
  lcd.print(6*count);
  lcd.setCursor(0,1);
  lcd.print(radiationValue);
  
 attachInterrupt(0,countPulse,FALLING);


startEthernet();
graph.fileopt="overwrite"; // See the "Usage" section in https://github.com/plotly/arduino-api for details
bool success;
success = graph.init();
if(!success){while(true){}}
graph.openStream();
}
unsigned long x;
int y;

void loop(){

 if (millis()-timePreviousMeassure > 10000){
    countPerMinute = 6*count;
    radiationValue = countPerMinute * CONV_FACTOR;
    timePreviousMeassure = millis();
    Serial.print("cpm = "); 
    Serial.print(countPerMinute,DEC);
    Serial.print(" - ");
    Serial.print("uSv/h = ");
    Serial.println(radiationValue,4);      
    lcd.clear();    
    lcd.setCursor(0, 0);
    lcd.print("CPM=");
    lcd.setCursor(4,0);
    lcd.print(countPerMinute);
    lcd.setCursor(0,1);
    lcd.print(radiationValue,4);
    lcd.setCursor(6,1);
    lcd.print(" uSv/h");
    count = 0;
  
  }
   
   sensorValue = analogRead(probePin);  // take a reading from the probe
   
      lcd2.setCursor(0, 0);
      lcd2.print("EMF=");
      lcd2.setCursor(5, 0);
      lcd2.print(sensorValue);   // Show a 0
      lcd2.setCursor(0, 1);
      lcd2.print("0=Low - 600=High");
   delay(3000);
   lcd2.clear();
  }
  

 void countPulse(){
  detachInterrupt(0);
  count++;
  while(digitalRead(2)==0){
  }
  attachInterrupt(0,countPulse,FALLING);
 
 {
 graph.plot(millis(), analogRead(A0), tokens[0]);
graph.plot(millis(), analogRead(A1), tokens[1]);
delay(50);

 }
 }

Just browsing through your code I notice that you have

attachInterrupt(0,countPulse,FALLING);

Interrupt 0 will respond to changes on Pin 2. Which seems correct as you make a digital read of pin 2 within your function countPulse()

But in an earlier reply you mentioned

Ethernet shield uses pin 2

So it's not just your LCDs conflicting with the ethernet card. It's just possible that your ISR is hanging due to the ethernet adapter holding pin 2 low.