Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member

Arduino kits for beginners contain plenty of sensors. Mine contains a temperature sensor, an ambient light sensor, buttons, flex sensors, potentiometers and so on. You can write programs that read the sensors. But what do you do next? You either want to share the readings somehow, or you want your microcontroller to do something based on the readings. Have a look at the photo below - this is something I built to demonstrate the idea of controlling robotic arms. We will not go into details yet (how about we build it from scratch in one of the next blog posts?), but what you can see is a potentiometer (in the middle of the breadboard) that is being read by Arduino, and the readings are then used to control a servo mechanism in the robotic arm. In other words, when you turn the potentiometer the robotic claw opens and closes. Sometimes you might just want to send the readings to an external system - for instance, when you want to build a network of sensors.

Sending data from Arduino over a serial connection

Let's start with an easier case: sending output data to a computer. In order to demonstrate it, I have built a super simple circuit. A potentiometer connected to an Arduino:

The next step is to write some code that reads the value of the potentiometer and sends it over the serial connection (using the USB cable). Here is the sample code:

Does it get any simpler than that? In case things are unclear: in the first function we set up the serial connection, with 9600bps. And then, we continuously read a value coming from analog pin 0 (that's where the potentiometer is connected to) and print this value to the serial connection. We then wait 1/10th of a second and repeat. That's it.

After deploying the code to Arduino, all we have to do is open Serial Monitor (the "magnifying glass" icon in the top right corner of the IDE), and we can see the values! (I was moving the potentiometer right before taking the screenshot, so we get all sorts of values popping up.)

Making Arduino listen to you

Making Arduino listen to your computer is not that hard either. Let's consider an example in which we want to turn an LED on and off with a click of a mouse. Nothing easier. Here we go.

Code to run on your Arduino

First, here is the Arduino code. It is super simple

Step 1

We listen to the values on the serial connection. We are using the built in LED (Arduinos have one LED, 13, on the board, it's really good for testing - I mentioned that in the previous blog post). And we make it clear to Arduino that our PIN no 13 is set to output.

Step 2

We listen for a serial connection. If there's a value coming, we check the value (the "-'0'" part is important so that we convert a character representing the digit to the actual integer). If the value is 0, we turn the LED off. If it is anything more than that (well, we should only get '1'), we turn the LED on.

int LED = 13;
void setup() {
      Serial.begin(9600);
      pinMode(LED, OUTPUT);
}
void loop(){
      while (Serial.available() > 0) {
           int num=Serial.read()-'0';
           if(num<1){
                digitalWrite(LED, LOW);
           } else{
                digitalWrite(LED, HIGH);
           }
      }
}

Simple and easy, right? Now we need to take care of the other end.

Code to run on your computer

You can use a programming language of your choice to send values to Arduino over the serial port. I am going to use the Processing language and its own IDE. I find it really useful for simple prototypes.

All you need to do is go to the Processing webpage, download the IDE and paste the following code in the editor. Also make sure that the Serial Monitor in the Arduino IDE is closed (Processing will try to access serial port, and it will not be very happy when it finds out that someone else is blocking access).

Here is the Processing code, and I'll explain it below.

import processing.serial.*;
Serial com;
boolean led=false; //set LED to off
void setup(){
      com = new Serial(this, Serial.list()[8], 9600);
}
void draw(){
}
void mousePressed() {
      led=!led;
      if(led){
           com.write('1');
      }else{
           com.write('0');
      }
}


First, we import libraries that will help us communicate with the serial port. 'com' is going to be our serial port, and we also set a boolean variable led to false (this will be our LED state). In the setup function we initiate the serial connection. Note: you need to find your serial port. On Windows machines it should be the first one available ([0]), on my Mac it was the 9th one. How did I find out? Maybe there's an easier way, but I went to Arduino IDE, Tools|Serial Port, and found my connection listed as the 9th one. So I put [8] (the 9th item in the vector) and it worked straight away!

draw() is normally used to draw windows in Processing. We don't need it, so we leave it empty.

And then we have a mousePressed() triggered when, well, a mouse button is pressed. Here, we toggle the value of led, and send either '1' or '0' over the serial connection.

When you execute the code, a grey window will pop up. Remember to click your mouse inside the grey window!

Brace yourselves!

And here is probably the most boring YouTube video ever! A result of our experiment on making Arduino listen to us. It shows the end result of the two pieces of code working together. When I click the mouse, built-in LED 13 turns on and then off. I don't know how about you, but the first time I got it to work, I was super excited!

If you look very closely, you will see the RX LED blinking - you can actually see the bytes being sent to the Arduino when I click the mouse!

What's next?

We have two routes to take from where we are now. We can either try to make HANA talk to the Arduino, or make Arduino control some more sophisticated hardware, such as a robotic arm. Or, how about Leap Motion controlling a robotic arm? Feel free to suggest the direction in the comments below. Or just stay tuned and wait to see what comes to my mind! Like I wrote in the first blog post: I have no idea what I am doing. But I am definitely having fun in the process. I hope you are having fun too!

5 Comments