Creating a UART Slave Device in Atmosphere

Written by Atmosphere Senior Software Engineer Nicholas Crast We’ve received quite a few requests about how to implement a slave device via UART using Atmosphere, and today we’ve got a simple project that demonstrates how to do it. It’s quick and easy to connect an Atmosphere-supported development board to an existing infrastructure by implementing a UART slave protocol, and it’s done by using Atmosphere Studio’s
regular expression (regex) element and
UART element.
Hardware Setup
This project requires an ESP32 and another UART-capable development board. For this demo, we’ll be using an Arduino 101, although any Arduino-supported board should work with only minor code modifications. Make the following connections between the ESP32 and Arduino 101:
- Connect pin 0 (Rx) of the Arduino 101 to IO14 (Tx) of the ESP32
- Connect pin 1 (Tx) of the Arduino 101 to IO15 (Rx) of the ESP32
- (Optional) Connect an analog input source to pin IO35 of the ESP32
- (Optional) Connect an LED to IO19 of the ESP32
When the connections are complete, your setup should resemble the following:

After the hardware is setup, the Arduino needs to be programmed separately with its own project, which requires access to the Arduino IDE. Within the Arduino IDE, add the following code to a new Arduino project:
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial1.begin(115200);
delay(10000);
Serial.println("Arduino starting...");
}
void loop() {
Serial1.write("{adc}");
Serial.println("ADC: " + Serial1.readString());
delay(3000);
Serial1.write("{toggle}");
delay(3000);
}
NOTE: On the Arduino101, Serial
refers to the built-in serial over USB. Serial1
refers to pins 0 (Rx) and 1 (Tx) which are connected to pins IO14 (TX) and IO15 (RX) on the ESP32. Now that the hardware setup is complete, we can build the Atmosphere project.
Creating the Atmosphere Project
Start by creating a new ESP32 project in Atmosphere Studio. The project built in Atmosphere will respond to two commands:
{adc}
: Take a reading from the ADC on pin IO35{toggle}
: Toggle the GPIO pin IO19
Creating the project only takes a small number of steps.
1. Configuring the UART element
In Studio’s Embedded View, add a UART element to the canvas. Set its properties as follows:
- Instance:
2
- Baud Rate:
115200
- Buffered Reading Regex:
{.*}.*
- Data Type:
String
This configures the UART driver to buffer all incoming traffic until a string surrounded by {}
is received. This will be our command format. Once a string is received, the next step is figuring out which command (if any) is being sent. This can be done using the regex element.
2. Configuring the ADC command regex element
Now add a regex element to the canvas. This element’s job is to determine whether or not the {adc} command has been received. Set the properties as follows:
- Pattern:
{adc}.*
Next, connect the UART element to the regex element. When these elements are connected, by default the UART element’s Data Received
trigger is set with the regex element’s ability Evaluate
. This means that when a string surrounded by {}
is received, it will be sent to the regex element to determine if it’s the {adc} command.
At this point, you should have something like this:

Now, what happens if the {toggle}
command is received?
3. Configuring the toggle command regex element
If the previous ADC regex evaluation fails (meaning there was not a match) then it will execute the Pattern Does Not Match
trigger, passing along the string. Using this, we can chain regex elements together. Add a second regex element into the canvas. This element’s job will determine whether or nor the {toggle}
command has been received. Set the properties as follows:
- Pattern:
{adc}.*
Next, connect the first (ADC) regex element to the second one (toggle). In the connector’s properties, set the trigger to Pattern Does Not Match
of the ADC regex element to the Evaluate
ability of the toggle regex element. If the ADC regex evaluation fails (meaning the string does not match) the string will be passed to the Toggle regex element, where it will be evaluated again. At this point, your setup should resemble the following:

So we have these two commands, how about we make them do something?
4. Reading an ADC Pin
Add an

ADC pin element into the canvas, and set the properties as follows:
- Instance:
0
- Pin:
IO35
- Number of Samples:
1
Connect the ADC regex element to the ADC pin element, then in their connector set the Pattern Matches
trigger of the ADC Regular Expression element to the Read Voltage
ability of the ADC element. At this point, your setup should resemble the following:
At this point, when the {adc}
command is received, the ADC is read. The next step is to send that data back to whichever element sent the command.
5. Responding with ADC reading
Before we send a response, the ADC reading must be converted into a string. Add
a data type conversion element into the canvas, and set the properties as follows:
- Output Type:
String
Now connect the ADC pin element to the data type conversion element. Select it’s connector, and within the Voltage Read
trigger of the ADC element, set the ability to Convert
. Next, connect the data type conversion element back to the UART element. Select its connector, and set the Converted
trigger of the data type conversion element to the Write Blocking
ability of the UART element. This converts the ADC reading to a string and then sends it over UART. At this point, your setup should resemble the following:

That takes care of the ADC. The last thing to tackle is the toggle command via GPIO.
Setting the GPIO toggle
Lastly, add a
GPIO pin element to the canvas, and set the properties as follows:
- Instance:
0
- GPIO Pin:
IO19
- GPIO Mode:
Output Push Pull
- Initial State:
Low
- Enable Interrupt:
False
- Interrupt Mode:
None
Connect toggle regex element to the GPIO pin element, and select it’s connector. Set the Pattern Matches
trigger of the toggle regex element to the Toggle
ability of the GPIO pin element. This means when the regex evaluation passes (pattern matches), the GPIO pin is toggled. You should now have your project complete, which looks something like this:

Running the demo
Compile and program your finished ESP32 project, and then start the Arduino project after the ESP32 project has booted. On the Arduino serial monitor, you should see an ADC reading every few seconds. To learn more, or to try a slightly more advanced version of this demo, head over to Atmosphere Studio and open the “UART Slave Demo” example project. This demo implements the base of this project while also integrating some cloud features.