HOW TO USE SERIAL TO TRIGGER PIN?

Hello tinkerers,

I have searched all around and can't find how to do a simple thing on arduino.

All I want to do is trigger pin 16 high as soon as there is serial data on RX. When there is no data, trigger pin 16 back to LOW.

This is what I want to do but cannot.

void setup()
{
  Serial.begin(115200);  
  pinMode (16, OUTPUT);
  digitalWrite (16, LOW);
}

void loop(){


    
  if (Serial.available());
   digitalWrite(ledPin, HIGH);


if  (!Serial.available() ) 


{digitalWrite(ledPin, LOW);

}
    }

This should be straight forward but not so, I have tried about a dozen different ways, with mills and delays, serial.end, serial.read... but can't get LED to turn on after turning off?

What direction should I take.

It does not matter what data is on serial and I don't need to read it, all I need is when serial data is coming in, then trigger pin high, when not low.

if (Serial.available());
The ; is a very very bad thing to have there.

Where do you read the data?

Where is ledPin defined?

Don't just put lines of code and braces wherever. There is a common way to format your code where the blocks and braces all line up. We don't do that just to make it pretty. It will often times help you to find bugs. But at least it is considerate to others who have to try to read it. The IDE will even do the work for you if you hit control-T so there's really no excuse for having your code wander all over the page. Get into that habit now and I promise you'll thank me later.

Hint

  while (Serial.available() > 0)
  {
    digitalWrite(ledPin, HIGH);
  .
  .
  .

  }

If you never read the data from the serial port once one byte arrives that is it, the serial available call will always return a value of more than zero. It can never get back to zero again.

The line

if (Serial.available());

Will always do nothing because you have a semicolon at the end, thus ending your if statement without doing anything.

mixamode:
All I want to do is trigger pin 16 high as soon as there is serial data on RX. When there is no data, trigger pin 16 back to LOW.

As well as what the others have said you will need to clear the data from Serial f you want your trigger to go back to low. You can do that with

void loop() {
  if (Serial.available() >0 ) {
     digitalWrite(triggerPin, HIGH);
     while(Serial.available() >0 {
        Serial.read();
     }
  }
  else {
    digitalWrite(triggerPin, LOW);
  }
}

However you should consider that carefully as I suspect it is not what you actually need. You have not told us what you project is trying to do.

…R

When you do get it to work the trigger pin will only be high for a very very small fraction of a second. Do you know this?

Grumpy_Mike:
When you do get it to work the trigger pin will only be high for a very very small fraction of a second. Do you know this?

That is why I wrote the closing sentence in Reply #6.

...R

Thanks for all your replies,

yes, I must admit the sketch I posted was copy paste and delete parts here and there for the forum for the mere fact that I was getting no where with all my sketches, I have about 10 different ones saves but don't want to bore you all with that....

The sketch was meant to only show the idea, I know its like bad grammar to an English professor for which I profusely apologize.

With the pin triggering for a short period I could add a delay, which I know you guys would say is bad practice, but my project is such....

I want to use attiny to trigger bootloader mode for an esp8266, that is- when attiny sees serial connecting to RX it will hold gpio0 low, reset pin low for a short while, then release back to high and so on causing esp8266 to go into bootloader mode.

Why? I am using HC 05 bluetooth to program esp automatically. The HC 05 only has RX and TX, I know EN pin goes high or low upon active serial, but can't use because it will block serial communication.

hc 05 sharing serial TX with RX esp8266 and (software serial RX) attiny

source:Serial Communication - learn.sparkfun.com

The only other place i saw someone mention was here: "Making ESP8266 Programming Easier"

Where attiny is used for auto programming esp8266, but his board has several other things that I don't need.

void setup()
{
  Serial.begin(115200); 
  pinMode (16, OUTPUT);
  digitalWrite (16, LOW);
}




void loop() {
  if (Serial.available() >0 ) {
     digitalWrite(16, HIGH);
     while(Serial.available() >0) {
        Serial.read();
        delay (500);
     }
  }
  else {
    digitalWrite(16, LOW);
  }
}

seems to work when delay added, but led not going back on until about 3mins later even though no serial communication through bluetooth?

without delay led just stays on. (Led is active low).

Seems like a simple concept at first but in reality it is not, at least to me.

I just need to figure out how to turn one pin on/off while there is serial, then I think I can figure out the rest.

Classic XY problem... you should have stared by your goal.

Where is the attiny connected? Can you describe the full set up - your computer, arduino IDE? Etc

Robin2:
That is why I wrote the closing sentence in Reply #6.

...R

yes I know and when I add a delay it keeps blinking. How do I avoid that, and say if there is data on serial hold pin high for x amount of time and then low? Do I use millis? as in blink without delay example?

J-M-L:
Classic XY problem... you should have stared by your goal.

Where is the attiny connected? Can you describe the full set up - your computer, arduino IDE? Etc

Attiny RX (software serial) is connected to RX on Esp8266 and TX on HC 05
Attiny PWM to gpio 0 on Esp8266
Attiny PWM to reset on esp8266

hco5 rx connected to tx on esp8266

Then power supply and common ground.

Arduino ide on windows paired through Bluetooth to hc 05, (port 25). Nothing connected to PC except through Bluetooth.

Thats about it.

Attiny needs to send pin "a" low then pin "b" low then both pins back high once when attiny sees data coming on serial rx.

Robin2:
As well as what the others have said you will need to clear the data from Serial f you want your trigger to go back to low. You can do that with

void loop() {

if (Serial.available() >0 ) {
    digitalWrite(triggerPin, HIGH);
    while(Serial.available() >0 {
        Serial.read();
    }
  }
  else {
    digitalWrite(triggerPin, LOW);
  }
}




However you should consider that carefully as I suspect it is not what you actually need. You have not told us what you project is trying to do.

...R

I am not sure if you are the author of an identical reply I read somewhere in this forum:) yes, in this project the attiny upon receiving serial data triggers two pins low (in a sequence), then both high thereby placing esp8266 in sketch upload mode.

The serial data's actual destination is the esp8266, not the attiny. And depending on if it is a sketch coming through RX (on attiny), places esp8266 into flash mode. If it comes to tx it bypasses the attiny (see photo above), thereby you can have esp8266 send serial data to arduino ide without placing esp8266 into flashmode.

mixamode:
I am not sure if you are the author of an identical reply

To the best of my recollection I have not written a Reply identical to what is in Reply #5

...R

you need to ground the gpio0 only for the reset. so a delay will do it.

    pinMode(ESP_GPIO0_PIN, OUTPUT);
    digitalWrite(ESP_GPIO0_PIN, LOW);
    pinMode(ESP_CH_EN_PIN, OUTPUT);
    digitalWrite(ESP_CH_EN_PIN, LOW);
    delay(5);
    pinMode(ESP_CH_EN_PIN, INPUT); // let it to esp module pull-up resistor
    delay(50);
    pinMode(ESP_GPIO0_PIN, INPUT); // let it to esp module pull-up resistor

set a boolean variable true after you detected data on Serial and make the reset.

in some intervals read all data from Serial, wait 100 ms and check if more data are still available, if not the flashing is over. set the boolean flag to false

BUT, why you do not use Arduino OTA?

Juraj:
BUT, why you do not use Arduino OTA?

Thanks for the reply, I will try what you say,

In the mean time to answer your question with a question- can you use arduino serial monitor with OTA and esp8266? What I mean is that to my knowledge you cannot sent data from esp8266 to arduino serial monitor, say you want to read time on esp8266, can this data be sent to serial monitor OTA? I don't think so, besides the fact that the first sketch you always have to connect usb serial to esp8266 to upload OTA, not to mention memory limits with OTA... just a few I can think of I am sure there are more...

... another factor, which I don't want to sound funny about is why did Espressif come out with esp32?

mixamode:
can you use arduino serial monitor with OTA and esp8266?

I use Eclipse IDE. there is the port for the Serial Monitor independent from the upload port. But the truth is I do not use SM, because I log to telnet.
you can open a second Arduino IDE, if only the port selection is the problem.

mixamode:
What I mean is that to my knowledge you cannot sent data from esp8266 to arduino serial monitor, say you want to read time on esp8266, can this data be sent to serial monitor OTA?

OTA telnet

mixamode:
the fact that the first sketch you always have to connect usb serial to esp8266 to upload OTA,

that is true, but bluetooth? it is a usb cable length range. a ftdi with dtr will do.

mixamode:
not to mention memory limits with OTA...

valid argument, but only with very small flash memory

I work with Uno WiFi. It connects on-board chips ATmega328 to ESP8266 on Serial1 (an additional on board UART connected to Atmega as I2C device)

On UNO WiFi you can't disconnect the ESP from ATMega and connect it to USB adapter for flashing like you do it with a module. The solution is a software bridge, a sketch EspProxy in ATmega which copies bytes from USB to ESP and back.

Flashing tools send DTR signal to USB chip to trigger the reset of the processor to bootloader. The signal causes the reset of the ATmega with the EspProxy sketch. And EspProxy calls in setup() a reset or a reset to bootloader of ESP. This way the ESP is in bootloader mode if the tool or IDE requires it.

EspProxy detects esp flashing sync frame and resets ESP to botloader.