Spark (3.3v) to Arduino (5v) Serial Communication

This is in reference to another thread, but I got a Spark talking via serial to my Arduino Uno using a logic level converter (I actually used one from eBay, but it works the same). The goal of this little test is to see if I could write from the Spark to the Arduino and listen for any serial data that the Arduino might generate. It’s pretty simple and straightforward. Since my test Arduino is an Uno, I’m limited to SoftwareSerial, so I had to scale down the baud rate. I will eventually tap the Spark into an Arduino Mega which has multiple hardware serial ports. The code is below the fancy graphics.

Hardware setup:

Sent strings using spark-cli:

Received strings on Arduino:

Here’s the Spark code:

void setup() {
	Spark.function("sendData", sendData);

	Serial.begin(9600);
	Serial1.begin(9600);
}


void loop() {
	if(Serial1.available())
		Serial.write(Serial1.read());
}


int sendData(String command) {
	Serial.print("INCOMING: ");
	Serial.println(command);

	Serial1.print("SPARK: ");
	Serial1.println(command);

	return 1;
}```

The Arduino code:

#include <SoftwareSerial.h>

SoftwareSerial ser(2,3);

void setup() {
Serial.begin(9600);
Serial.println(“SERIAL: BEGIN”);

ser.begin(9600);
ser.println(“SOFTWARE SERIAL: BEGIN”);
}

void loop() {
if(ser.available())
Serial.write(ser.read());
}```

1 Like

Yup, good work! :smile:

Just a quick note: You don’t really need the logic level converter. The TX pin from the Core doesn’t need anything special, it can go straight into the Arduino’s RX pin; Now, for the TX pin coming from the Arduino, you can simply connect a 10K resistor between it and the Core’s RX pin to prevent it from sucking in too much current and you’ll be fine.

1 Like

It is recommended to include a logic level converter if the pins are not 5V tolerant as is the case with TX/RX pins on Core. Even with a resistor to limit the current in the circuit won’t help. It is the over-stress voltage across the thin gate oxide within the IC which is a serious danger to mixed-voltage I/O interface circuits. There’s a possibility of permanent failure when operated continuously over a period of time.

But a voltage devider would help - I guess :wink: (as mentioned in @wgbartley 's own other thread dealing with the very same topic)

I’d like to propose to join these two threads to make it easier for others to find answers and not to have this info scattered all over different threads with no new info.

Anecdotal Evidence:

That is true and the best practice, but personally I’ve never seen it happen with a modern MCU. I’ve got an MSP430 that’s been running a year and a half with just a resistor and a Raspberry Pi about a year. (Plus various other MCUs and a few Propellers, which are notoriously sensitive.)

For a permanent installation I’d use a logic level converter (TI makes nice ones) or a FET if you want to go the simple/cheap route. But again, and I stress this is just personal experience, I’ve never had any issues just limiting the current.

Logical Evidence:

I also don’t think you get 5V from the TX pin if you use a high enough value resistor. Let’s say the 3V3 RX can sink 20mA; once you limit the current flow to < 20mA the voltage should drop. (Assuming the TX pin can source > 20mA.)

I know on open drain interfaces you can’t get away just a resistor, but I’m pretty sure you can on push-pull ones, based on the above. (At least in the short term.)

timb, I’m with you but I believe a safe recommendation is needed. This whole arduino-ish 5V signals to the Spark needs to be put on the FAQs so requirements are clear. :smile:

I'm not sure exactly what you meant, but I don't think it works like you are describing.

Arduino TX most likely has a CMOS fet to 5V, so it's always going to be driving at 5V regardless of the current.

The reason you might see the voltage drop at the Core's RX input, is because all I/O pins typically have clamp diodes to VDD and GND. Limiting the current flow will lessen the Vf drop across that diode. The resulting voltage at the input would be VDD + Vf. I'm not sure how the core does this with it's 5V tolerant pins, but the TX/RX pins are not 5V tolerant so it doesn't matter in that case.

The Core's RX input won't sink any current... well ok, maybe microamps. It's the clamp diode that will sink the current to VDD if the input is > VDD + Vf.

I simple two resistor voltage divider also should work pretty well for serial at lower baud rates. It might tend to round off a little at 115200, if the arduino can even do that with software serial (I didn't think it worked any higher than 57600).

FYI: For the 5V tolerant pins, the STM manual states you should disconnect any internal pullups or pulldowns if you are running 5V on any pin. This is not related to this discussion really, but I thought I'd mention it.

I tried to do some Arduino <--> RaspberryPi serial communications using a voltage divider, but could never get anything to work well, which is why I went straight to using the logic level converter.

I started at 115200, but the text didn't come across as what I sent it. I assumed the software serial couldn't handle that rate, so I dropped it to 9600 to be extra safe. I'm going to try it with the Arduion Mega hardware serial this weekend and see how well everything handles at 115200.

If you set your resistor divider to a low enough impedance, it should provide a nice square waveform. The Arduino can drive pins at 40mA as well… so don’t be afraid to drive it at 10mA let’s say.

5V / 10mA = 500 ohms total.

This works pretty well with a 200 ohm on the high side, and 330 ohm on the low side. Even at 5.25V in, you get slightly less than 3.3V out.

5V / 530 = 9.4mA

Oh hey! I can control my 3D printer wirelessly now!

The “INCOMING:” lines are what is sent to the Spark using Spark.function(), and the “ok:” lines are the response from the 3D printer.

  • M104 S230 sets the hot-end temperature to 230C.
  • M105 gets the current temperatures.

Now that the proof-of-concept is working, it’s time to make a fancy web interface for it.

1 Like

What’s 3D printer is that! :smiley:

It’s a Prusa i3 kit from 3dprinterczar.com. It’s fairly simple if you have a RAMPS board running Marlin firmware. You can’t use an LCD+SD card with it as it takes up the extra hardware serial pins. I’m going to see if I can work around it, so I can have wifi and the LCD+SD card at the same time. I think the SPI pins are still available, but I haven’t investigated the SPI functionality (if any) exists in the Marlin firmware.

In the interim, I’m going to build out the necessary back-end/infrastructure to buffer/transmit g-code from a web page to the Core. I might start a new thread about it instead of stuffing it in this one.

:D. Dang!

You’re making me want to save up some pocket money for this :stuck_out_tongue:

I highly recommend it. However, buy from a reputable vendor in your country of choice. Given the problems I’ve had (missing parts, missing/terrible instructions, SLOW shipping), I don’t recommend ordering from China if you are within the USA. After waiting over a month to receive additional hardware that I’m sure was never sent, I had to source the missing parts myself for ~$50. Then my hot-end melted down, and I had to order a new one of those last week (another $80 after S&H).

However, I do recommend DIY. It really helps you learn and appreciate how these things work. If you get one with a RAMPS board, try to find one fully assembled, unless you enjoy the tedium of soldering dozens of pins. I like my Prusa i3 quite a bit, but I’m already thinking of saving up for a delta printer as well. The design of the deltas just fascinates me more than anything. It seems more “elegant” with more wow factor.

Also, if you have a baby (~6 months), watching a very methodical 1st-layer infill (with 2cm+ fill lines) will easily put them to sleep. It’s mesmerizing and a little relaxing.

1 Like

Rocking out to Metallica always worked for me :metal:

However, this 3D printer argument would be great for wives that don't let their husbands buy fancy tech :smile:

1 Like

Are there wives that do let their husbands buy fancy tech?

An update on this. It looks like you need to have something constantly reading the serial data, at least if it’s connected to serial 0 on the Arduino Mega / RAMPS board running the Marlin firmware. If you don’t, or you have something even hooked up but unpowered, the 3D printer will stall very frequently, leaving blobs in your prints. It took me a while to figure that one out. I had disconnected power from the Core, but left the TX/RX wires still connected to the serial 0 pins on RAMPS. One I disconnected those wires, the remainder of my print went smoothly.

I will try a different set of serial pins and see if that helps, since serial 0 is also shared with USB in RAMPS.(Marlin firmware).

2 Likes

I have been trying to get serial from Arduino to Spark but I’m not sure what I’m doing wrong. I followed the example on OP only changing the serial ports to (0,1) in Arduino code. I am also not using a logic level converter but a 10 k resistor. Here is my setup:

and the code:

Arduino:

#include <SoftwareSerial.h>

SoftwareSerial ser(0,1);

void setup() {
  Serial.begin(9600);
  Serial.println("SERIAL: BEGIN");

  ser.begin(9600);
  ser.println("SOFTWARE SERIAL: BEGIN");
}


void loop() {
  if(ser.available()) {
    Serial.print("writing to serial");
    Serial.write(ser.read());
  }
}

Spark Core:

Same as OP

Hi,
I have a little problem, when I’m sending data from the Spark to the arduino, the data I receive are just number. Exemple I send “allo”, I’m going to receive this: “97 108 108 111”. And I can receive anything coming from the arduino except if an switch the Rx and Tx cable. Any idea what is going on ?

Thanks

What you get are bytes and the byte value 97 stands in ASCII for the letter a (108 = l, 111 = o).
I guess you do Serial.print() instead of Serial.write() the received bytes.
Serial.print() does not actually print out the bytes you pass in but does convert them into the decimal representation of the byte value.
Try to write the byte :wink:

If the concept of ASCII and bytes and stuff if fairly new for you, you might like to have some programming primers from these Harvard University Computer Science videos (see timeline 33:50 onward)

2 Likes