MenuToggle Menu

Using Arduino with ProtoPie Connect

Create multi-screen experiences across both software and hardware thanks to ProtoPie Connect’s built-in Arduino plugin.

ProtoPie Connect supports serial communication with Arduino boards. The most typical setup would be connecting the Arduino hardware to the machine where ProtoPie Connect is running via USB.

Learn more about how to use Arduino.

Connecting Arduino to ProtoPie Connect via USB

1. Select Arduino in ProtoPie Connect’s Plugin list.

[object Object]

2. Select the desired Port and Baud Rate:

  • Port: Select the port corresponding to your Arduino board.
[object Object]
  • Baud Rate: This value determines how frequently the serial connection will be checked for updates. You can select the default value 9600.
[object Object]

*Please note that the Arduino plugin works with any Micro Controller communicating via serial. For example—if you want to use an ESP32 microcontroller, after connecting the ESP32—you can select the appropriate port and baud rate to open the serial port ESP32 and receive data in ProtoPie Connect.

Using Arduino with ProtoPie Connect

ProtoPie Connect and Arduino communicate using a Message||Value format. If the intention is to send a message without a value, Message would suffice.

Sending messages from Arduino

Use the Serial.println() function to send messages (and values) to ProtoPie Connect, which then communicates them to all corresponding prototypes.

In the following example, Arduino sends the message ROTATE and value 90 every 2 seconds to ProtoPie Connect.

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

void loop() {
  // Send "ROTATE" to ProtoPie
  // message: ROTATE
  // value: 90
  Serial.println("ROTATE||90");
  delay(2000);
}

Sending messages to Arduino

The Arduino hardware requires a separate code to interpret incoming messages in the Message||Value format.

In the below example, Arduino receives and interprets messages received from ProtoPie Connect.

#include <string.h>

// Declare struct
struct MessageValue {
  String message;
  String value; // Note that value is of String type
};

// Declare function that parse message format
struct MessageValue getMessage(String inputtedStr) {
  struct MessageValue result;

  char charArr[50];
  inputtedStr.toCharArray(charArr, 50);
  char* ptr = strtok(charArr, "||");
  result.message = String(ptr);
  ptr = strtok(NULL, "||");

  if (ptr == NULL) {
    result.value = String("");
    return result;
  }

  result.value = String(ptr);

  return result; 
}

// Declare MessageValue struct's instance
struct MessageValue receivedData;

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

/*
		if you want to make waiting time for reading serial data short,
		set waiting time with `Serial.setTimeout` function.
	*/
	Serial.setTimeout(10);
}

void loop() {
// Take out strings until Serial buffer is empty
	while (Serial.available() > 0) {
// From ProtoPie Connect 1.9.0, We can use '\0' as delimiter in Arduino Serial
		String receivedString = Serial.readStringUntil('\0');

		receivedData = getMessage(receivedString);
  }

	// Do something with received message from ProtoPie Connect

	if (receivedData.message.equals("FIRST")) { // If message from ProtoPie Connect equals "FIRST" do the following 
		l1 = receivedData.value.toInt(); // receivedData.value.toInt() converts the value from ProtoPie Connect to integer type and assigns it to l1
		analogWrite(firstLED, l1); 
	} 
}

Use Cases

Try recreating the following use case to understand better how Arduino works with ProtoPie Connect.

Control Your Home Lights

Turn the lights on and off consecutively. Test this yourself using an Arduino board connected to ProtoPie Connect.

1. Add this prototype to ProtoPie Connect.
2. Set up your Arduino board and the light controls following this circuit diagram.

[object Object]

3. Connect your Arduino board to ProtoPie Connect.
4. Use this example code to send messages from Arduino to ProtoPie Connect.

#include <string.h>

struct MessageValue {
  String message;
  String value;
};

struct MessageValue getMessage(String inputtedStr) {
  struct MessageValue result;

  char charArr[50];
  inputtedStr.toCharArray(charArr, 50);
  char* ptr = strtok(charArr, "||");
  result.message = String(ptr);
  ptr = strtok(NULL, "||");

  if (ptr == NULL) {
    result.value = String("");
    return result;
  }

  result.value = String(ptr);

  return result;
}

int firstLED = 3;
int secondLED = 5;
int thirdLED = 6;
struct MessageValue receivedData;

void setup() {
  pinMode(firstLED, OUTPUT);
  pinMode(secondLED, OUTPUT);
  pinMode(thirdLED, OUTPUT);
  Serial.begin(9600);
  Serial.setTimeout(10); // Set waiting time for serial data to 10 milliSeconds
}

void loop() {
  while (Serial.available() > 0) { // Take out strings until Serial is empty
    String receivedString = Serial.readStringUntil('\0'); // From 1.9.0 version, We can use '\0' as delimiter in Arduino Serial
    receivedData = getMessage(receivedString);
  }

  if (receivedData.message.equals("FIRST")) {
    analogWrite(firstLED, receivedData.value.toInt());
    delay(30);
  } else if (receivedData.message.equals("SECOND")) {
    analogWrite(secondLED, receivedData.value.toInt());
    delay(30);
  } else {
    analogWrite(thirdLED, receivedData.value.toInt());
    delay(30);
  }
}

Surgical Robot Arm

Learn how to prototype a robot arm (Arduino Braccio robot arm) controlled by a wireless controller.

Check out this article to explore the practical application of ProtoPie in robotics prototyping.

Code used in Arduino.

Back To Top