Jump to content

SERCOM fun in Arduino


Recommended Posts

  • Member

I am working on a project for my camping trailer.  The trailer has a 200W solar panel charging a 100AH LiFe battery.  This crazy amount of energy is used to run lights, a refrigerator and, more importantly, a laptop and telescope while I am in the field.


There is the trailer stored in the yard.  The grey metal on the front is a set of drawer slides holding the 200W 24v panel.  The 24v works well for the MPPT's because I get charging voltage earlier in the morning.  The 200W works well because I don't want to move the panels to track the sun while I am camping.  Now I want a status panel for the entire trailer in my home office so I can see how prepared I am for camping at moments notice.

When I bought the trailer I specified a custom electrical system.  This system utilizes a lot of Victron Energy equipment because it works well and it has a serial port with a published specification.  They even show off open source projects using their equipment.

Electrical box

Here is the electrical box.  It did get a bit tight.  Each of the blue units is a device with serial data for monitoring.  This box lives in the black equipment box on the trailer tongue.

The serial port is called VE.Direct and it is a simple 1 second transmission of useful information at 19200 baud.  Here is an example of a transmission recorded from the trailer.

PID     0xA053
FW      146
V       13500
I       -180
VPV     39200
PPV     0
CS      5
MPPT    1
OR      0x00000000
ERR     0
IL      200
H19     2436
H20     21
H21     106
H22     27
H23     132
HSDS    117
Checksum        $

I decided that step 1 in my project is to collect the data from the 3 devices in the box and report them to a second human interface/radio link in the back of the trailer.  Here is my plan:


This indicates that I need an MCU that has 5 serial ports and is easily programmed for my Hobby.  I have been doing quite a bit of Arduino work in Platform IO and that combination is pretty good so how about using one of the many SAMD21 based Arduino kits.  The SAMD21 has 6 SERCOM's that can be used for this purpose.

So the first step... How easy is it to use 5 of the SERCOM's to do what I want?  Not too bad but there are a few gotcha's.

The SAMD21 Arduino support has a file called variant.cpp that defines a big array of structures to cover the capabilities of each pin.  This structure includes a "default configuration" setting such as SERCOM, Analog, Digital, Timer, etc... When you open the serial port (Serial.begin(19200)) the serial library puts the pins into the "default" behavior and not the SERCOM behavior.  Therefore if you want to use multiple sercom's you must do a few steps.

1) Ensure your SERCOM is initialized with the correct I/O pins.

Uart ve1(&sercom2, sc2_rx, sc2_tx, SERCOM_RX_PAD_3, UART_TX_PAD_2);
Uart ve2(&sercom3, sc3_rx, sc3_tx, SERCOM_RX_PAD_1, UART_TX_PAD_0);
Uart ve3(&sercom1, sc1_rx, sc1_tx, SERCOM_RX_PAD_3, UART_TX_PAD_2);
Uart ve4(&sercom4, sc4_rx, sc4_tx, SERCOM_RX_PAD_1, UART_TX_PAD_0);

The tricky bit here is mapping the Arduino PIN number, to the SAMD21 Port/PIN numbers so you can determine which portmux settings are applicable.  This is helpfully documented in the variant file but it is tedious especially since I build a custom PCB and had to keep track of every abstraction layer to ensure I hooked it up correctly.

2) Start the UARTS.


This step was VERY simple and straight forward.

3) Put the new pins in the correct mode.

  pinPeripheral(sc1_tx, PIO_SERCOM);
  pinPeripheral(sc1_rx, PIO_SERCOM);
  pinPeripheral(sc2_tx, PIO_SERCOM);
  pinPeripheral(sc2_rx, PIO_SERCOM);
  pinPeripheral(sc3_tx, PIO_SERCOM);
  pinPeripheral(sc3_rx, PIO_SERCOM);
  pinPeripheral(sc4_tx, PIO_SERCOM_ALT);
  pinPeripheral(sc4_rx, PIO_SERCOM_ALT);

The begin() function put each of these pins in their DEFAULT mode according to the variant table.  But most of these pins did not have PIO_SERCOM as their default mode.  You must call pinPeripheral() after you call begin() OR change the variant table.

Naturally, my initial code did not but things did not work.  Since I am working on this project off and on for some time, I found the bug in my VE.Direct class!  The constructor was issuing the begin() which does make sense but it breaks the UART configuration that was already up and running.

Now my serial ports are all running, and I am playing recorded messages to the system so I can debug inside where it is not 40C.  On to the next problem.

Good Luck

  • Like 2
Link to comment
Share on other sites

  • 4 weeks later...

Hi...when we first checked out the ATSAMD21 chip, we were very happy to see that the chip has 6 "SERCOM"s, a.k.a SERial COMmunication modules. Each one of these modules can be used for I2C, SPI or Serial. That means you can have 3 UART's & 3 SPI's, or maybe 2 SPI's and 4 I2C's. Basically, you have a ton of options for adding more hardware support for the most common 3 chip-to-chip protocols.

  • Like 1
Link to comment
Share on other sites

  • Member

Exactly!  Lots of serial ports can be very useful in a variety of situations.  Using these serial ports in the Arduino environment had a few challenges that the standard tutorials did not completely cover so I hope the Arduino SERCOM information I provided here is useful.


Link to comment
Share on other sites

Nice project! How about adding some non-Victron nodes like environmental sensors to the system?

Good to see you've thrown out that old lead battery, LiFe batteries are so much better! I have mine for 2 years now and it still has full (60 Ah) capacity. Discharged it twice down to 9.8 V, nothing happened.

Link to comment
Share on other sites

  • Member

I have decided that I need dimmable lights in the cabin and the ability to load shed in a low power situation.

The next feature will be a smart fuse block with individual BPS443 power switches for each circuit.  Then I can turn off the refrigerator and dim the cabin lights at the fuse panel.  Normally that would not be fantastic but in this case I simply want to bias the lights down a few percent so this will be fine.  I won't use the SAMD21 for this project for other reasons which I will write about.


Link to comment
Share on other sites

  • Member

In case anyone was curious, and pictures or it did not happen.  Here is the completed data collection unit.

The engraving was with my laser.  The PCB image is really a ChipKit (just a handy DXF I had).  Unfortunately the laser water flow sensor had a leak so the tube overheated and lost power most of the they through the engraving.  This has all been fixed and apparently no permanent damage.  Oh well.  It still looks nice.  Front and back panels are laser cut acrylic.  Blue extrusion is from here: https://www.amazon.com/dp/B07MPX23FS/ref=cm_sw_em_r_mt_dp_U_QPQ4Eb3MH0BNX

The mounting flanges are these:  https://www.amazon.com/dp/B07MZPFWKK/ref=cm_sw_em_r_mt_dp_U_BQQ4Eb6BE926B


  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Create New...