Go Down

Topic: Help with serial read (Read 1 time) previous topic - next topic


Hi, I am trying to control the analog output of a pin in Arduino but without success

here is the code:

int test;
void setup()

void loop(){
 while (serialAvailable()){
   test = serialRead();      //read Serial        

The analogWrite cant read the variable test, I dont know why. I have tryed with different kinds of variables and different scripts but no luck at all. Can anyone help???

Many thanks!


How are you sending data to the board?  If you're using the Arduino serial monitor and typing in numbers, the data will be sent as ASCII digits which all have similar values (e.g. character 0 has ASCII code 48, character 1 has ASCII code 1, etc).  So you'll always write a similar value to the pin.  


Hi mellis,

yes, I am using the Arduino Board monitor interface. I think the problem is that if I send for example "100" the "test = serialRead()" will probably read the character sent one at a time. So if I want to output 100 as a parameter for analogWrite, I should probably create an array, save all the ASCII string and then output the value as a variable inside the analogWrite function. I have tried, but it doesn't work.

Shall I specify that the variable test is ASCII?

Many thanks for you answer.


The easiest thing is probably to find a way to send raw bytes to the Arduino board, for example with a simple Processing sketch.  Or, if you're okay with a limited range of values you can send various characters from the Arduino serial monitor: e.g. 0 (which has an ASCII value of 48) to z (which has an ASCII value of 122).


Many thanks again for the answer.... I made it work and I have written a tutorial so other people can achieve my same result.

You can find it here:

Thanks again!

Go Up