# How to read the value of each sensor from the Arduino

**URL:** <https://discourse.processing.org/t/how-to-read-the-value-of-each-sensor-from-the-arduino/36914>\
**Category:** Processing.py\
**Created:** [May 17, 2022, 8:37am UTC](https://discourse.processing.org/t/how-to-read-the-value-of-each-sensor-from-the-arduino/36914 "2022-05-17T08:37:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![The-NUll](https://avatars.discourse-cdn.com/v4/letter/t/8baadc/32.png) [@The-NUll](https://discourse.processing.org/u/The-NUll)\
**Post date:** [May 17, 2022, 8:37am UTC](https://discourse.processing.org/t/how-to-read-the-value-of-each-sensor-from-the-arduino/36914/1 "2022-05-17T08:37:53Z")

</div>

Hi, I’m new to Arduino and Processing.py.

I am using Processing and Arduino to complete my graduation project.(English is not my native language, sometimes I speak it’s very strange.)  
I want to implement the **Serial library** so that Processing can read different values of different sensors on arduino.I tried to create an ever-changing list of sensor values using split().  
I tried to use split() to separate the incoming values, but I kept getting an error. And I don’t know how to use bufferUntil() properly. 😭 😭

Here is my Arduino and Processing code.Can someone help me?

```auto

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:

  int val0 = analogRead(A0); //
  int val1 = analogRead(A1);
  int val2 = analogRead(A2);
  int val3 = analogRead(A3);
  int val4 = analogRead(A4);
  int val5 = analogRead(A5);

  Serial.print(val0, DEC);
  Serial.print(',');
  Serial.print(val1, DEC);
  Serial.print(',');
  Serial.print(val2, DEC);
  Serial.print(',');
  Serial.print(val3, DEC);
  Serial.print(',');
  Serial.print(val4, DEC);
  Serial.print(',');
  Serial.print(val5, DEC);
  Serial.print('\n');
  // Serial.println();
  delay(1000);

}

```

```auto
add_library("serial")
myPort = Serial(this,"COM3",9600)

def setup():
    size(200,200)
    myPort.bufferUntil('\n') 
    
def draw():
   
    if myPort.available()>0:
        P_dates = myPort.readString()
        string01 = P_dates
        TRY = string01.split(",")
        C = list()
        for i in TRY:
            C.append(int(i))
        println(C)
        
    delay(1000)
    
def serialEvent(Serial):
    inString = p.readString()

```

These are some of the mistakes I’ve encountered

```auto
TypeError: bufferUntil(): 1st arg can't be coerced to int

```

If I delete bufferUnitl ()

```auto
ValueError: invalid literal for int() with base 10: '77 51'

```

When I changed the program, the output list became strange

```auto
if myPort.available()>0:
        P_dates = myPort.readString()
        string01 = P_dates
        TRY = string01.split(",")
        C = list()
        for i in TRY:
            C.append((i))
        println(TRY)
        

```

I don’t understand why the output value is like this.The values on the serial monitor are correct.  
The console values are as follows

```auto
[u'49', u'56', u'53', u'53', u'123', u'80\n']
[u'48', u'56', u'53', u'53', u'89', u'60\n']
[u'49', u'55', u'53', u'53', u'94', u'60\n']
[u'49', u'56', u'53', u'53', u'110', u'64\n']
[u'48', u'55', u'52', u'52', u'103', u'60\n']
[u'47', u'55', u'53', u'53', u'91', u'57\n']

```

---

<div class="post-metadata">

**Author:** ![villares](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/villares/32/3166_2.png) [@villares](https://discourse.processing.org/u/villares)\
**Post date:** [May 17, 2022, 1:58pm UTC](https://discourse.processing.org/t/how-to-read-the-value-of-each-sensor-from-the-arduino/36914/2 "2022-05-17T13:58:05Z")

</div>

Cheers @The-NUll !

I’m afraid I can’t test/explore your code right now, but I’ve been using Firmata to read the values from Arduino, maybe this project can be of use for you:

- [lousa-magica/LousaMagica.pyde at master · villares/lousa-magica · GitHub](https://github.com/villares/lousa-magica/blob/master/LousaMagica/LousaMagica.pyde)
- [lousa-magica/README-EN.md at master · villares/lousa-magica · GitHub](https://github.com/villares/lousa-magica/blob/master/README-EN.md) (for context)

Beware that on some platforms the Serial library is not working with Processing.py ☹

---

<div class="post-metadata">

**Author:** ![The-NUll](https://avatars.discourse-cdn.com/v4/letter/t/8baadc/32.png) [@The-NUll](https://discourse.processing.org/u/The-NUll)\
**Post date:** [May 17, 2022, 3:37pm UTC](https://discourse.processing.org/t/how-to-read-the-value-of-each-sensor-from-the-arduino/36914/3 "2022-05-17T15:37:58Z")

</div>

Thank you very much! You gave me a new idea to try! I hope it helps me. 🥰

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [May 17, 2022, 10:21pm UTC](https://discourse.processing.org/t/how-to-read-the-value-of-each-sensor-from-the-arduino/36914/4 "2022-05-17T22:21:47Z")

</div>

Hello,

I am new to Python as well.

I tried this and it worked:

```auto
add_library("serial")
myPort = Serial(this,"COM5",9600)

def setup():
    size(200,200)
    myPort.bufferUntil(0x0A) 
    frameRate(10);
    
def draw():
    background(255, 0, 0); // I needed something here
    
def serialEvent(Serial):
    inString = myPort.readString()
    println(inString)
    inString = trim(inString)
    arr = inString.split(",")
    for i in arr:
        print(i)

```

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/7/a/7ac85d39051193bffbf3b46e10240993d59cadb2.png)

`:)`

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [May 17, 2022, 10:34pm UTC](https://discourse.processing.org/t/how-to-read-the-value-of-each-sensor-from-the-arduino/36914/5 "2022-05-17T22:34:14Z")

</div>

> [@glv](#):
>
> `myPort.bufferUntil(0x0A) `

You can replace literal `0x0A` w/ constant [ENTER](http://processing.github.io/processing-javadocs/core/processing/core/PConstants.html#ENTER):  
`myPort.bufferUntil(ENTER) `

BtW, I recommend taking a look at my Java Mode sketch example too:

> [@Using two different readStringUntil "characters"](https://discourse.processing.org/t/using-two-different-readstringuntil-characters/10769/6):
>
> The solution I gave on that link expects the Arduino C code to send the multi-data as if they were a row of tab-separated values (TSV): face_with_monocle That is, each value is followed by a \t character; except last 1, which is followed by an \n instead: nerd_face Serial.print(temperature); Serial.write('\t'); Serial.print(masse); Serial.write('\t'); Serial.print(humidite); Serial.write('\t'); Serial.print(comptage); Serial.write('\t'); Serial.println(son); // implies '\n' At the Pro…

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [May 17, 2022, 10:41pm UTC](https://discourse.processing.org/t/how-to-read-the-value-of-each-sensor-from-the-arduino/36914/6 "2022-05-17T22:41:18Z")

</div>

Just realized I’ve already had a Python Mode example for the Serial library, but for a single value: 🐍

> [@Read Serial Value (From Arduino)](https://discourse.processing.org/t/read-serial-value-from-arduino/5829/2):
>
> This is my lousy attempt to convert an old Java Mode sketch from the link below to Python Mode: snake[Forum.Processing.org/two/discussion/16618/processing-with-arduino-void-serialevent#Item\_5](http://Forum.Processing.org/two/discussion/16618/processing-with-arduino-void-serialevent#Item_5) """ \* Efficient Serial Reading (v1.01) \* GoToLoop (2018-Nov-22) \* \* Discourse.Processing.org/t/read-serial-value-from-arduino/5829/2 \* \* Forum.Processing.org/two/discussion/16618/ \* processing-with-arduino-void-serialevent#Item\_5 """ add\_library('serial') PORT\_INDEX, BAUDS = 0, 9600 myString =…

---

<div class="post-metadata">

**Author:** ![The-NUll](https://avatars.discourse-cdn.com/v4/letter/t/8baadc/32.png) [@The-NUll](https://discourse.processing.org/u/The-NUll)\
**Post date:** [May 19, 2022, 11:54am UTC](https://discourse.processing.org/t/how-to-read-the-value-of-each-sensor-from-the-arduino/36914/7 "2022-05-19T11:54:06Z")

</div>

Thank you. I solve my problem, now it works well 😃 😃

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [June 11, 2022, 9:15pm UTC](https://discourse.processing.org/t/how-to-read-the-value-of-each-sensor-from-the-arduino/36914/8 "2022-06-11T21:15:53Z")

</div>

> [@GoToLoop](#):
>
> You can replace literal `0x0A` w/ constant [ENTER](http://processing.github.io/processing-javadocs/core/processing/core/PConstants.html#ENTER):  
> `myPort.bufferUntil(ENTER)`

> [@GoToLoop](#):
>
> Just realized I’ve already had a Python Mode example for the Serial library

I tried this and responded here:

> [@Read Serial Value (From Arduino)](https://discourse.processing.org/t/read-serial-value-from-arduino/5829/3):
>
> It does not work. This will work with Processing 3.5.4 Python mode: # Python Serial Reading # v1.0.0 # GLV 2022-06-11 add\_library("serial") myPort = Serial(this, "COM2", 9600) inString = "" def setup(): size(400, 100) # This does not work: #myPort.bufferUntil(ENTER) # This works: myPort.bufferUntil(0x0A) textSize(24); textAlign(LEFT, CENTER); fill(0); noLoop() def draw(): background(255); # I needed something here text (inString, 5…

`:)`

---

<div class="post-metadata">

**Author:** ![JSGauthier](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jsgauthier/32/9910_2.png) [@JSGauthier](https://discourse.processing.org/u/JSGauthier)\
**Post date:** [June 15, 2022, 3:59pm UTC](https://discourse.processing.org/t/how-to-read-the-value-of-each-sensor-from-the-arduino/36914/9 "2022-06-15T15:59:37Z")

</div>

What was the solution?
