Arduino Mega 2650 w5100 and DS18B20 Not working


I have an Arduino mega and after clearing the eeprom, I hook up 2 dallas DS18B20s on the same digital pin 51. After clearing the eeprom I can go to my Dallas sensor library and test. My serial port shows both sensors and they are reading correct Farenheit temps. The problem comes when I stack my wifi w5100 on the arduino… I load the cayenne sketch for the w5100 into the mega and I cannot get any reading from my temp sensors. When I look at the serial monitor it says found 0 devices. I have installed a relay shield on digital pins 42 and 43 also, and they work great. I can also turn the relays on and of from my smart phone app. CuThe other issue I am having is that the ethernet shield disconnects a lot from the arduino. Sometimes it will reconnect, sometimes not. This is getting pretty annoying. How do I get the cayenne widget to read two different sensors, and get the correct Farenheit reading on both?


Hi Brett,

Apologies for the frustration. Can you share the sketch you’re using that isn’t working for you so I can look for anything that sticks out there? Remember edit out any personal information like Cayenne token.

I’ll set up my Mega with a couple of DS18B20s to see if I can replicate, or if I can get this to work, something to share with you that definitely works on my end.


So right after I clear the eeprom I Hook up my 2 sensors to digital pin 51 on the arduino with the 4.7k ohm resistor and all of that… I get this…


As soon as I stack the w5100 shield on the mega and digital pin 51 still connected to temp sensors, I get this on the serial port. This is just stacking with no code.


I then proceed to put the following code in for the W5100 from the Cayenne library… The w5100 connects, but when I try to connect a widget in cayenne dashboard for this, it doesn’t work.


I can however add a relay shield, and I didn’t add any code. Just added relay widget and specified pins. I can turn the 2 relays on the shield on and off. So that part is working.


After adding the W5100 shield, you need to add code to gather values from each of the pins, and then add widgets to your dashboard which receive data from the virtual pins specified in your code. Here is what is working for me with a W5100 and 2 DS18B20s. Just a few notes first:

  • I have the sensors wired to digital pins 5 & 6 on the shield. I was having trouble getting a reading from pins 50 and 51 on my Mega, and wanted to be able to share something with you tonight.

  • I have the widgets on my dashboard set to Virtual pins 9 and 10 to match this code

  • There are some println statements in my code that were there for debug, they can be removed.

Having real trouble getting the forum to make my code look pretty tonight, so here is a pastebin :slight_smile:


The reason that I am needing pin 51 is that I am going to be using cayenne to monitor and control my saltwater reef tank, and the Dallas temp sensor circuit is connected to pin 51. You said you were having issues, is it possible to do it on pin 51? Also how would you get multiple sensor readings off of the single pin? I’m thinking that you would have to code in the addresses of the sensors from the first serial port pic I posted? I will actually need to monitor three DS18b20 sensors on that one pin 51… Water temp, sump temp, and light fixture temp.


Tried that code, and I’m reading -196.6 degrees Fahrenheit.


Also wondering if there is a good wifi shield out that isn’t retired? I was looking at the wifi 101 but its retired… I am thinking wifi would be a better way to connect to my network vs an ethernet connector with wifi adapter.


There is no reason (that I’ve found yet) why things shouldn’t work on Pin 51, though what you posted is what I was seeing as well. I’m also not sure that the code I posted is going to behave with multiple sensors on the same pin. I’ll play with this some more tomorrow. I think even if I can’t get Pin51 to work here, I can get you connected using our MQTT client so you can pass data on a virtual channel regardless of pin.

Thanks for your patience, I’m sure I can this working the way you prefer.


Thanks a TON!!!


Ok, after some trial and tribulation today I have this working! With just one caveat:

Pin 51 on the Mega appears to be a special SPI pin, and I can’t get my code to work on that pin. If you can move things around just a little, this works fine on Pin 49 (and 3, 5, 6, 8 that I tried, likely the vast majority of them). Whichever pin you choose, make sure to set it in this segment:

// Digital pin where both sensors are connected on the Arduino Mega
#define ONE_WIRE_BUS 49

This code used our MQTT Arduino Library rather than our standard Arduino Library, so make sure you have that installed in your IDE as well or you’ll see compile errors. For that reason, you’ll need to re-add the device on your Cayenne dashboard through Add New > Device / Widget > Bring Your Own Thing. That page will provide the MQTT Username/Password/Client ID to fill in the sketch below.

Finally, the sensor addresses in my code below are unique to my sensors – I used this code to determine them, you’ll need to do the same. And a final thank you to the author of this code which I used pieces of to make my sketch below.

#define CAYENNE_PRINT Serial
#include <CayenneMQTTEthernet.h>
#include <OneWire.h>
#include <DallasTemperature.h>

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "yourMQTTUsername";
char password[] = "yourMQTTPassword";
char clientID[] = "yourMQTTClientID";

unsigned long lastMillis = 0;

// Digital pin where both sensors are connected on the Arduino Mega
#define ONE_WIRE_BUS 49

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

DeviceAddress tempSensorA = { 0x28, 0xFF, 0x30, 0xCA, 0x64, 0x15, 0x02, 0x7A };
DeviceAddress tempSensorB = { 0x28, 0xFF, 0x01, 0xB0, 0x64, 0x15, 0x02, 0x80 };

void setup() {
	Cayenne.begin(username, password, clientID);
  //set the resolution to 10 bit (good enough?)
  sensors.setResolution(tempSensorA, 10);
  sensors.setResolution(tempSensorB, 10);

void loop() {

	//Publish data every 10 seconds (10000 milliseconds). Change this value to publish at a different interval.
	if (millis() - lastMillis > 10000) {
		lastMillis = millis();
		//Write data to Cayenne here. This example just sends the current uptime in milliseconds.
    Cayenne.celsiusWrite(9, sensors.getTempC(tempSensorA));
    Cayenne.celsiusWrite(10, sensors.getTempC(tempSensorB));

    //Print Temperatures to the Serial Montior (optionally)
    Serial.print("Sensor A temperature is: ");
    Serial.print("Sensor B temperature is: ");

//Default function for processing actuator commands from the Cayenne Dashboard.
//You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
	CAYENNE_LOG("CAYENNE_IN_DEFAULT(%u) - %s, %s",, getValue.getId(), getValue.asString());
	//Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");

void printTemperature(DeviceAddress deviceAddress)
  float tempC = sensors.getTempC(deviceAddress);
  if (tempC == -127.00) {
    Serial.print("Error getting temperature");
  } else {
    Serial.print("C: ");
    Serial.print(" F: ");

:sweat_smile: By all means let me know if you have questions/trouble, I think after troubleshooting this so much for myself today I’m in a good position to catch little gotchas.


My serial port says connected, but the dashboard won’t come online, and I am unable to add relay shield, or any other widgets. The temp sensors are working and talking to the dashboard on my PC… It comes online and then goes offline randomly… The widgets on my Cayenne App on my phone all read 25 degrees F and they don’t move like the ones on the PC dashboard. I am still unable to add widgets like I did on the regular dashboard since switching to the MQTT dashboard… I had two relay buttons that turned on and off perfectly both by the button on the PC and the smartphone app, that aren’t working now. I’m also going to need sliders,controls, and monitors for 7 channels of LED lights. My LEDS are controlled via PWM and go to LDD drivers.


These are the pins that are in the reef controller code… My LED PWM digital outputs are pin 7-13 My relays need to go on pins 42,43,52, and 53. Is it possible to use pin 51 in the reef controller program, and run a jumper from that to another pin that Cayenne will pickup? Or will that interfere and confuse the microprocessor?


I understand a bit better now, apologies, I thought you were building a project from scratch components like sensors and actuators, and didn’t realize you had a manufactured shield of this type, so I didn’t think it would be a big deal to switch pins.

I assume in addition to any Cayenne code, it needs to be running this or something similar?

What I’d probably do is take a step back, ignore specific sensors and actuators for a moment, and and see if you can combine that (or whatever similar monster you have) with our example client code. Just to see if you can get the device connected to the Cayenne dashboard without any random connection/disconnection.

I know our regular Arduino code can be sensitive to long delays which cause it to lose connectivity with our server, I wonder if that’s what’s happened here as well. I’m tagging @jburhenn for his opinion on whether the MQTT Arduino code might equally be sensitive to time delays which might exist in non-Cayenne code.

Also, to save you a headache, if you’re using the Cayenne Android app, I’d say don’t, until you have this working as you prefer from our web dashboard. There are a number of open issues with MQTT actuators here, and sync issues between web and mobile. We’re hard at work to improve these, but it will be easier to troubleshoot and test if you’re only working between the device, your sketch code, and Cayenne, without throwing a 4th variable into the mix.


That is definitely the sketch I am using… However, I have to use IDE version 1.0.4 to load the sketch because I was getting a bunch of errors and was told that the newer IDEuses a different syntax. I would really love to be able to get Cayenne working with this so I can monitor my tank 24/7… If it’s going to be a huge hassle, there are other alternatives I may pursue, but I’d really like to use Cayenne. Eventually the phone app is what I’m ultimately after, but I’ll take your advice and deal with the pc dashboard first… This seems like a pretty big task, I hope you can help me. :slight_smile:


A last bit, I decided to search Jarduino and one of the first things that came up was this thread: Arduino Online/Offline Override

May be worth asking the users there what sort of progress they were able to make (especially @noriel.instructor) as I might be running you into walls or directions that they already tackled


So I know very little about writing code, and I can’t seem to get the example client code into the Jarduino v1,2beta2 without creating a nightmare of errors. Jamie (Author of Jarduino) told me that I had to use IDE 1.0.4 to load the sketch to keep from getting errors. Which i had done before. So not sure how I will be able to load from that IDE when it doesn’t have the Cayenne library. Update… I loaded the Jarduino sketch, and then loaded the example client code after with the latest IDE… I am going to leave it hooked up overnight to see if it disconnects… So far it has been running for about 15 minutes and I have the serial port up to see if it disconnects… So far so good. Connected once and still going. I don’t have anything hooked to the board, just the Mega and the ethernet shield. Let me know what you need me to do after this. Thanks. Brett


It is almost 2:00 PM the following day, and my serial port is showing connected right now, but the dashboard is offline as well as the app… First pic is from about 10:00 this morning… Second pic is currently…