Go Down

Topic: NMEA 2000 Shield (Read 230565 times) previous topic - next topic


Where do you need that?


Where do you need that?
I'm building a fast-packet message that needs a 32 bit signed int.


The point was that normally that data is logically real value (double) and then you just have multiplier 1. Add4ByteDouble(val,1) does that background.


Nov 05, 2019, 04:58 pm Last Edit: Nov 05, 2019, 05:55 pm by LiliumJSN
Hello everyone,
First of all, i have to thank timolappalainen for this great lib.
I am having trouble using the lib in listener mode.
I am using an Αrduino MEGA2560 with mcp2515-tja1050 module.
Using the tempmonitor example and "#define USE_MCP_CAN_CLOCK_SET 8",
i can write to the bus succesfully and read the messages from an NGT-1 device
using NMA Reader, and i can also read the messages forwarded from the Arduino
to serial using the NMA Reader. So, i assume that the hardware is connected correctly.
Into the bus there is a Raymarine iTC-5 analog converter (with wind instruments
attached), an NGT-1 gateway, and the mega with the can module.
I can read the values of the Raymarine instruments correctly using the NGT-1,
but using the ActisenseListener i get no messages in NMA Reader.
If i uncomment SetForwardType(tNMEA2000::fwdt_Text), i just get "CAN Ready" message.
I have also define correctly the interrupt pin from the module.
Has anyone used 2560 and tja1050 successfully as a listener?
I am out of debug ideas.


Hi Timo...

I noticed that I can't get GetStr() to work. Am I using it wrong? For example:

This code does not work...

Code: [Select]
char strBuf[255];
N2kMsg.GetStr(strBuf, 255, Index); 

This code does work...

Code: [Select]
  char ch = 0;
  for(int i=0; i<30; i++)
    ch = N2kMsg.GetByte(Index);

    if(ch == NULL)


THere are different strings in N2k. If data is fixed length string you use GetStr. I prefer the version, where you provide also destination length. So e.g., if you know data is 20 char string on N2k, then use.
char strBuf[255]:
N2kMsg.GetStr(255, strBuf, 20, 0xff, Index);

If you read variable length string use
char strBuf[255]:
size_t bufSize=255;
N2kMsg.GetVarStr(bufSize, strBuf, Index);

In bufSize function returns read length.

So you need to know, what type of string data contains.


THere are different strings in N2k.
Ah, I understand, thanks for explaining. I'll give it a try.


Hi Timo

I seem to recall in an earlier post you said there was some issue using the wire.h library. Possibly to do with delays. Can you expand on that or advise what needs to be changed.



Can you point my reply?

I use on teensy i2c_t3 library, which is replacement for wire library. There I can make "request" and handle result on callback.

In generally since Arduino IDE or Teensyduino does not offer multitasking, one should take care that any "task" (like NMEA2000.ParseMessages()) you call does not stop processing for waiting long time response from some other device. This may happen with wire library but also commonly with SerialX - Serial is normally USB and faster handling.

I N2k you can get in worst case 25 frames in 10 ms. So if other part of you program may stop for 40 ms, you need input frame buffer of length 100 frames to ensure that you will not miss any frames. Naturally this is worst case scenario. Anyway this means that I had to split e.g. graphics functions to parts to keep max off time to 40 ms.


Can you point my reply?

Post #451 here https://forum.arduino.cc/index.php?topic=50893.450 It may be I don't understand what the issue is. I have used the one-wire library with DS18B20 and it seems all ok.


I replied that, but can not see my reply.

As default DallasTemperature library does everything with delays. When it sends "measure temperature" command to 1-wire bus, it then waits with delay until sensors has done temeperature measurement. For 12-bit DS18S20 that tkaes 700 ms, which means that you will not call NMEA2000.ParseMessages during that time. So you may miss some important requests from other devices and they may then drop you from the list. And when you send your temperature data to bus, they will see you and request your information again. This may lead to situation that your device will pop off/on on some MFD:s.

You can also use DallasTemperature library in async mode. Then it just sends the request and you can keep your own timer and after 700 ms start to read values. Also if you have several sensors, it is best to read them one by one on each loop round, since each read takes about 10 ms. Then you will not stop loop more that 10 ms and other tasks will be served.

If you will do lot of other things, even that delay is not good. I ended up to use DS2482-100 1-wire line driver, which does 1-wire timing for me. With that I used Teensy ic2_t3 library, which allows me asynchronous i2c communication. Finnally the code very little in total for reading temperatures.


Hello everyone,

I am new to this forum because of the great NMEA2000 library Timo programmed.

First of all I want to thank Timo for that great work!

I used this to program my first outside sensor for my boat. I have an Simrad NSS7 evo2 MFD and an Arduino Mega 2560 with a MCP shield on top. It works perfect but is a huge piece of electronics that I want to shrink to the minimum possible without the need to solder the Mega2560 onto a selfdesigned PCB.
So I looked at small Arduino boards and found the new nano every that seems to be perfect for this small sensor device.

Did someone try to use the nano every? Are there some information to share?

Thank you in advance.

Best regards, Martin


Forget Nano. Mega has 8 MB on memory and it is nearly minimum, where you run NMEA library and some other code. You can run it on Uno, if you disable several things from library.

I prefer to go to Teensy 3.2. It is small and fast and has 64 MB RAM. If you like, you can compile it to 24 MHz and run it with 13 mA from 12V. I had that as temperature monitor reading several DS1820 sensors and sending data to NMEA2000. And still would have plenty of flash and RAM for other things. Other good choice would be ESP32, but it has worse ADC and requires lot of corrections to reach 0.05 V accuracy over 30V.

For both Teensy and ESP32 you need only tranceiver (e.g. MCP2562) and DC-DC converter.

And about power I have to remind to avoid ground loops in your system. If that is risk, would be better to use isolated DC-DC and ISO1050 tranceiver.


Nov 17, 2019, 09:03 am Last Edit: Nov 17, 2019, 09:24 am by Marten_83
Thank you for your reply, Timo.

I know your recommendations regarding the Arduino family. But the nano_EVERY is a bit more powerful than the normal nano board. It has 48kB of ROM, 6kB of RAM and a 20MHZ oscillator on board.
The possible SMD footprint is worth a try, too. Even if you do not recommend it, I will try it.


Well, I have a problem...

Compiling the code for the nano every, the following errors occurrs:

Code: [Select]
Arduino: 1.8.10 (Windows 7), Board: "Arduino Nano Every, None (ATMEGA4809)"

In file included from C:\Users\MartinS\Documents\Arduino\libraries\NMEA2000-master\src\N2kStream.h:38:0,

                 from C:\Users\MartinS\Documents\Arduino\libraries\NMEA2000-master\src\NMEA2000.h:48,

                 from C:\Users\MartinS\Documents\Arduino\libraries\NMEA2000-master\src\NMEA2000.cpp:24:

C:\Users\MartinS\Documents\Arduino\libraries\NMEA2000-master\src\NMEA2000.cpp: In function 'void CanIdToN2k(long unsigned int, unsigned char&, long unsigned int&, unsigned char&, unsigned char&)':

C:\Users\MartinS\AppData\Local\Arduino15\packages\arduino\hardware\megaavr\1.8.4\cores\arduino/Arduino.h:105:12: error: expected unqualified-id before numeric constant

 #define PF 5


C:\Users\MartinS\Documents\Arduino\libraries\NMEA2000-master\src\NMEA2000.cpp:852:17: note: in expansion of macro 'PF'

   unsigned char PF = (unsigned char) (id >> 16);


C:\Users\MartinS\Documents\Arduino\libraries\NMEA2000-master\src\NMEA2000.cpp: In function 'long unsigned int N2ktoCanID(unsigned char, long unsigned int, long unsigned int, unsigned char)':

C:\Users\MartinS\AppData\Local\Arduino15\packages\arduino\hardware\megaavr\1.8.4\cores\arduino/Arduino.h:105:12: error: expected unqualified-id before numeric constant

 #define PF 5


C:\Users\MartinS\Documents\Arduino\libraries\NMEA2000-master\src\NMEA2000.cpp:872:17: note: in expansion of macro 'PF'

   unsigned char PF = (unsigned char) (PGN >> 8);


Mehrere Bibliotheken wurden für "mcp_can.h" gefunden
 Benutzt: C:\Users\MartinS\Documents\Arduino\libraries\CAN_BUS_Shield-master
Mehrere Bibliotheken wurden für "NMEA2000_mcp.h" gefunden
 Benutzt: C:\Users\MartinS\Documents\Arduino\libraries\NMEA2000_mcp-master
Mehrere Bibliotheken wurden für "BME280I2C.h" gefunden
 Benutzt: C:\Users\MartinS\Documents\Arduino\libraries\BME280-2.3.0
Mehrere Bibliotheken wurden für "Wire.h" gefunden
 Benutzt: C:\Users\MartinS\AppData\Local\Arduino15\packages\arduino\hardware\megaavr\1.8.4\libraries\Wire
Mehrere Bibliotheken wurden für "NMEA2000_CAN.h" gefunden
 Benutzt: C:\Users\MartinS\Documents\Arduino\libraries\NMEA2000-master
Mehrere Bibliotheken wurden für "SPI.h" gefunden
 Benutzt: C:\Users\MartinS\AppData\Local\Arduino15\packages\arduino\hardware\megaavr\1.8.4\libraries\SPI
exit status 1
Fehler beim Kompilieren für das Board Arduino Nano Every.

Dieser Bericht wäre detaillierter, wenn die Option
"Ausführliche Ausgabe während der Kompilierung"
in Datei -> Voreinstellungen aktiviert wäre.

Especially the part "unsigned char PF = (unsigned char) (id >> 16);" I cannot find in the specific file.
There is "unsigned char CanIdPF = (unsigned char) (id >> 16);" and the compiler seems to misunderstand. Maybe I am wrong, but compiling for the Arduino Mega 2560 with the same program is compiled without errors and running on my hardware.

Does anyone can give me a hint what to do?

BR, Martin

Go Up