difficulty sending gcode to grbl controller from secondary circuit

Hi there!I have been trying to create a circuit which will allow me to controll my CNC machine(no room left on the atmega on the main circuit. i have managed to create one which works by connecting to the RX pin of the main circuit and sends the gcode commands via a TX pin. this works fine but whenever it is connected i am unable to send data over the main serial connection(the high state of the pin on my circuit is interferring). so i tried to set the TX pin to an INPUT when it is not needed. what i get is one good command and the rest are all messed up. I should mention that the code is running on an attiny85 which is why i am using software serial. many thanks.

#include <SoftwareSerial.h>
SoftwareSerial mySerial(0, 1); // RX, TX

#define switchPin A1 //analog pin which reads the switch resistor ladder
int switchVal;

//values for each button pressed
int upVal = 0;
int leftVal = 512;
int downVal = 682;
int rightVal = 768;
int middleVal = 819;
int spindleVal = 853;
int feed_upVal = 877;
int feed_downVal = 896;
int axis_toggleVal = 910;


// the setup function runs once when you press reset or power the board
void setup() {
  pinMode(switchPin, INPUT);
}

// the loop function runs over and over again forever
void loop() {

  switchVal = analogRead(switchPin);


  if (switchVal < 200) {
    //up pressed
    //    mySerial.println("up");
    pinMode(1,OUTPUT);
    mySerial.begin(115200);
    mySerial.println("g91g1y1f40");
    pinMode(1,INPUT);
  }
  if (switchVal > 200 && switchVal < 600) {
    //left pressed
    //    mySerial.println("left");
    pinMode(1,OUTPUT);
    mySerial.begin(115200);
    mySerial.println("g91g1x-1f40");
    pinMode(1,INPUT);
  }

gonadgranny:
Hi there!I have been trying to create a circuit which will allow me to controll my CNC machine(no room left on the atmega on the main circuit. i have managed to create one which works by connecting to the RX pin of the main circuit and sends the gcode commands via a TX pin.

Sorry, but I have no idea what all that means. You need to provide a lot more information.

What CNC machine?
What Atmega has no room? (And what does "no room" mean?)
What does "one" refer to?
What is the "Rx pin of the main circuit"
What is sending the gcode commands?
What Tx pin is it sending them with?

...R

Its a GRBL controlled 3d printed CNC machine. the controller board uses an atmega328p microcontroller. "no room" means that all the space on the chip has been used up by the firmware so i cant add anything else onto it (hence needing a new circuit). one refers to the circuit which i have created. the RX pin is the pin which serial data is received on the main controller. the microcontroller on the circuit which i have made is sending gcode commands from its TX (serial transmission) apologies if i was too concise i just thought that some of the terminology i was using was common knowledge.

Still much too concise. Don't worry, we don't charge by the word.

...R

i probably shouldnt have even mentioned the CNC machine as its seems to be serving as a distraction. the root of the problem is that while i am able to send serial data from an attiny85 to an atmega328p via pin 1 of the tiny to pin 30 of the atmega(RX pin), the serial data from my computer to the atmega (via usb to ttl ) is compromised. i tried 'switching off' the TX pin on the attiny85 by setting the pin as in input. this frees up the connection to my computer but the data from the attiny is garbled (apart from the very first command which is before i set TX pin to an input). does that make any more sense? thanks.

It makes a bit more sense.

But why are you trying to send data to Serial on the Atmega 328 from two different sources?

A diagram of what you are trying to do would make things a great deal clearer. For example how are the two different serial sources connected to the Atmega 328? See this Simple Image Upload Guide

...R

PS ... An Attiny seems a strange choice as a source of serial data considering how little memory it has.

Robin2:
It makes a bit more sense.

But why are you trying to send data to Serial on the Atmega 328 from two different sources?

A diagram of what you are trying to do would make things a great deal clearer. For example how are the two different serial sources connected to the Atmega 328? See this Simple Image Upload Guide

...R

PS ... An Attiny seems a strange choice as a source of serial data considering how little memory it has.

The circuit is acting as a means to control my CNC. i can do this from the computer but its in another room so i wanted something right next to the machine to make my life easier. the computer is connected to the atmega via a ch340 serial to TTL converter. the attiny is connected directly to the atmega's RX line. i chose an attiny because i only need to store a few gcode commands (up down left right, start spindle etc) and it has enough memory for that.

gonadgranny:
The circuit is acting as a means to control my CNC. i can do this from the computer but its in another room so i wanted something right next to the machine to make my life easier. the computer is connected to the atmega via a ch340 serial to TTL converter. the attiny is connected directly to the atmega's RX line. i chose an attiny because i only need to store a few gcode commands (up down left right, start spindle etc) and it has enough memory for that.

Your use of the term "circuit" is very confusing. Do you really mean that you are using an Attiny microcontroller to send some Gcode commands to the GRBL program to allow you to do some convenient manual control of the machine?

If your PC was using a USB-TTL cable to connect directly to the Rx and Tx pins (rather than the ch340) I could see a simple way to merge the inputs from it and from the Attiny. But if the PC is using the regular USB cable the signal gets to the Rx and Tx pins over the traces on the Arduino board.

The problem is that a serial line idles in the HIGH state which means that two connections to an Rx pin are both pulling the line HIGH and preventing each other from working.

Is it possible to convert to using a USB-TTL cable?

If so, the trick for feeding two separate signals into an Rx pin is to use a resistor (4k7 or 5k6 should be fine) to pull the Rx pin HIGH and have a diode on each of the inputs connecting to the Rx pin so that the HIGH on the input lines cannot get to the Rx pin but the lines can pull the Rx pin LOW.

Of course you must ensure that serial data does not appear on the two input lines at the same time - but I'm guessing that is not a problem.

...R

The problem is that a serial line idles in the HIGH state which means that two connections to an Rx pin are both pulling the line HIGH and preventing each other from working.

Is it possible to convert to using a USB-TTL cable?

If so, the trick for feeding two separate signals into an Rx pin is to use a resistor (4k7 or 5k6 should be fine) to pull the Rx pin HIGH and have a diode on each of the inputs connecting to the Rx pin so that the HIGH on the input lines cannot get to the Rx pin but the lines can pull the Rx pin LOW.

Of course you must ensure that serial data does not appear on the two input lines at the same time - but I'm guessing that is not a problem.

yes this is the problem thanks! so the cathode of the diodes would be connected to the data transmission pins?

Robin2:
Your use of the term "circuit" is very confusing. Do you really mean that you are using an Attiny microcontroller to send some Gcode commands to the GRBL program to allow you to do some convenient manual control of the machine?

If your PC was using a USB-TTL cable to connect directly to the Rx and Tx pins (rather than the ch340) I could see a simple way to merge the inputs from it and from the Attiny. But if the PC is using the regular USB cable the signal gets to the Rx and Tx pins over the traces on the Arduino board.

The problem is that a serial line idles in the HIGH state which means that two connections to an Rx pin are both pulling the line HIGH and preventing each other from working.

Is it possible to convert to using a USB-TTL cable?

If so, the trick for feeding two separate signals into an Rx pin is to use a resistor (4k7 or 5k6 should be fine) to pull the Rx pin HIGH and have a diode on each of the inputs connecting to the Rx pin so that the HIGH on the input lines cannot get to the Rx pin but the lines can pull the Rx pin LOW.

Of course you must ensure that serial data does not appear on the two input lines at the same time - but I'm guessing that is not a problem.

...R

i just added a diode as you suggested and it works! no pull up resistor or usb to ttl cable change needed....?
thanks very much for this.

Good to hear you have a solution.

Next time start with an accurate description of what you want to do :slight_smile:

...R