Serial communication

Can TX pin drive a transistor? I want to send 1010101 commands at a certain baud rate and I want the pin to go HIGH and LOW to drive a transistor. Is that possible? If not, what is the next best option.

Thanks.

That should be perfectly possible.

I think you would get a more useful answer if you explain what you want to achieve. In the normal way there would be no need for a transistor in serial communication. And I suspect the USART is not the ideal way to produce a stream of 1s and 0s

...R

I need to send 1s and 0s ( 1s meaning 5v, and 0s meaning 0v) depending on the analog input on A0.

at 1v A0 I need to send 010100000 then 10ms break then 010100000 then break 10ms etc.

at 2v A0 I need to send 001100000 then 10ms break etc.

0s and 1s need to be on for a 128 microseconds, I think I can adjust it with a baud rate.

I tried bit banging it on a digital pin using delays and it works great, but when I add analog read it doesn't work, probably due to delays.

Bit banging should work fine - but why not just transmit a standard serial character using Serial.write()

If you are thinking of using unusual baud rates you need to study the Atmega 328 datasheet (assuming you are using an Uno).

Post your code so we can give useful advice.

...R

const int analogPin = A0; 






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

void loop() {

    int analogValue = analogRead(analogPin);
  if (analogValue > 600 && analogValue < 1000) {
    
   digitalWrite(12, HIGH);   // turn the LED on (HIGH is the voltage level)  
  delayMicroseconds (128);              
  digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
 delayMicroseconds (128);
digitalWrite(12, HIGH);   // turn the LED on (HIGH is the voltage level)
  delayMicroseconds (896);
  digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
 delay (10);
  digitalWrite(12, HIGH);   // turn the LED on (HIGH is the voltage level)
  delayMicroseconds (1152);
   digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
 delay (10);
   digitalWrite(12, HIGH);   // turn the LED on (HIGH is the voltage level)
  delayMicroseconds (128);              // wait for a second
  digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
 delayMicroseconds (128);
digitalWrite(12, HIGH);   // turn the LED on (HIGH is the voltage level)
  delayMicroseconds (896);
    digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
 delay (100);
  }
  if (analogValue > 450 && analogValue < 570) {
  
    digitalWrite(12, HIGH);   // turn the LED on (HIGH is the voltage level)  
  delayMicroseconds (256);             
  digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
 delayMicroseconds (128);
digitalWrite(12, HIGH);   // turn the LED on (HIGH is the voltage level)
  delayMicroseconds (768);
  digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
 delay (10);
  digitalWrite(12, HIGH);   // turn the LED on (HIGH is the voltage level)
  delayMicroseconds (1152);
   digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
 delay (10);
   digitalWrite(12, HIGH);   // turn the LED on (HIGH is the voltage level)
  delayMicroseconds (256);              
  digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
 delayMicroseconds (128);
digitalWrite(12, HIGH);   // turn the LED on (HIGH is the voltage level)
  delayMicroseconds (768);
    digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
 delay (100);

}

else {
 digitalWrite(12, HIGH);   // turn the LED on (HIGH is the voltage level)  
  delayMicroseconds (256);              
  digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
 delayMicroseconds (256);
digitalWrite(12, HIGH);   // turn the LED on (HIGH is the voltage level)
  delayMicroseconds (640);
  digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
 delay (10);
  digitalWrite(12, HIGH);   // turn the LED on (HIGH is the voltage level)
  delayMicroseconds (1152);
   digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
 delay (10);
   digitalWrite(12, HIGH);   // turn the LED on (HIGH is the voltage level)
  delayMicroseconds (256);              
  digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
 delayMicroseconds (256);
digitalWrite(12, HIGH);   // turn the LED on (HIGH is the voltage level)
  delayMicroseconds (640);
    digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
 delay (100);
}


   
   

}

Here’s what I’ve tried. I need to send 1s and 0s with, 3 different packets 1152 microseconds with 10ms delays between and 100ms pause at the end.

I’d like to send that with TX pin since it’s a serial communication with a weird baud rate.

Thanks for the help.

electronicM:
I'd like to send that with TX pin since it's a serial communication with a weird baud rate.

You did not answer my request, in Reply #1, for a description of what you are trying to achieve - what is the project all about? What will be receiving this series of 1s and 0s etc etc etc.

Why are yu not using some sort of loop to produce your series of bits and save yourself a lot of typing with scope for errors.

...R

I am sending those 1s and 0s to an OEM dash.

Robin2:
You did not answer my request, in Reply #1, for a description of what you are trying to achieve - what is the project all about? What will be receiving this series of 1s and 0s etc etc etc.

Why are yu not using some sort of loop to produce your series of bits and save yourself a lot of typing with scope for errors.

...R

What kind of loop are you talking about? This was the first thing I came up with so I went with it.

Tell us some more about the OEM dash - does it have specifications and a datasheet?

If you want to send 10 bits why not do something like

for (n = 0; n < 10; n++) {
   // send one bit
}

…R

Robin2:
Tell us some more about the OEM dash - does it have specifications and a datasheet?

If you want to send 10 bits why not do something like

for (n = 0; n < 10; n++) {

// send one bit
}




...R

I doesn’t have a datasheet or any of specifications. factory ecu is communicating with it by sending bits of data ( 1s and 0s) for it to display things. I won’t be using a factory ecu any longer so I want arduino to collect analog values and send it to the dash to display things. I bit banged the data to the dash without any analog input just to see that’s it was correct data and it worked. As soon as I added analog read, it quit working, I am assuming the timing of the data transfer has changed. So I am trying to change things around to get it to work. It sends data with an if and else statement just fine, but as soon as I use multiply if statements, for some reason it just sends the data for else statement and last if statement, everything else isn’t working.

electronicM:
I bit banged the data to the dash without any analog input just to see that's it was correct data and it worked. As soon as I added analog read, it quit working,

Is the code in Reply#4 the version that works or the one that does not?

Please make sure we have both versions.

...R

const int analogPin = A0;






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

void loop() {

    int analogValue = analogRead(analogPin);
  if (analogValue > 600 && analogValue < 1000) {
   
   digitalWrite(12, HIGH);   // turn the LED on (HIGH is the voltage level) 
  delayMicroseconds (128);             
  digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
 delayMicroseconds (128);
digitalWrite(12, HIGH);   // turn the LED on (HIGH is the voltage level)
  delayMicroseconds (896);
  digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
 delay (10);
  digitalWrite(12, HIGH);   // turn the LED on (HIGH is the voltage level)
  delayMicroseconds (1152);
   digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
 delay (10);
   digitalWrite(12, HIGH);   // turn the LED on (HIGH is the voltage level)
  delayMicroseconds (128);              // wait for a second
  digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
 delayMicroseconds (128);
digitalWrite(12, HIGH);   // turn the LED on (HIGH is the voltage level)
  delayMicroseconds (896);
    digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
 delay (100);
  }


else {
 digitalWrite(12, HIGH);   // turn the LED on (HIGH is the voltage level) 
  delayMicroseconds (256);             
  digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
 delayMicroseconds (256);
digitalWrite(12, HIGH);   // turn the LED on (HIGH is the voltage level)
  delayMicroseconds (640);
  digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
 delay (10);
  digitalWrite(12, HIGH);   // turn the LED on (HIGH is the voltage level)
  delayMicroseconds (1152);
   digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
 delay (10);
   digitalWrite(12, HIGH);   // turn the LED on (HIGH is the voltage level)
  delayMicroseconds (256);             
  digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
 delayMicroseconds (256);
digitalWrite(12, HIGH);   // turn the LED on (HIGH is the voltage level)
  delayMicroseconds (640);
    digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
 delay (100);
}


   
   

}

Code only works if I delete one of the If statements like posted above. Code in post 4 doesn’t work and pretty much gets hung up on the else statement.

I will bookmark this to look at it tomorrow - too tired now.

...R

Robin2:
I will bookmark this to look at it tomorrow - too tired now.

...R

THanks for your help.

Try making

if (analogValue > 450 && analogValue < 570) {

into

else if (analogValue > 450 && analogValue < 570) {

As things stand, the ELSE is only connected to the second IF

If that does not solve the problem put in some code so you can see the value of analogValue and see which parts of the IF statements are being used.

…R

Robin2:
Try making

if (analogValue > 450 && analogValue < 570) {

into

else if (analogValue > 450 && analogValue < 570) {

As things stand, the ELSE is only connected to the second IF

If that does not solve the problem put in some code so you can see the value of analogValue and see which parts of the IF statements are being used.

…R

That’s what I ended up doing after serial comms wouldn’t work, it’s working. Thanks Robin2