Atmega328 RX & TX Pin as Input IO

Hello Friends!!

Please Help

I have tested my Code on UNO and working perfectly.(Except RESET Pin)
All of Arduino Pins are used including RX and Tx Pin as input.Both Rx & Tx was working as per my requirement and after satsfaction
I have developed Protype PCB. But now Tx pin misbehave.
Tx Pin=1, INPUT_PULLUP (Normal Case HIGH) and Push Button is connected One end to GND.
When Push button is pressed Tx pin supposed to work as per code (Same code as used on UNO) unfortunately it gets resetted.

What could be possible reason & how to fix it?

int sysPin = 1; // Dont change Pin number
int Buzzer = 0; // Dont change Pin number

Thanks,

Regards,

Rozex

Please post the schematic.

Please post the code within </> code-tags.

In general there is no reason why GPIO's 0 & 1 can not be used other than they may be used for other peripherals, specifically the UART. So without extra information i do not have a clue.

the guy asked for schematic. not pcb layout.

Schematic is not with me thats why PCB layout is shared

Then how was the PCB created ? The PCB layout is insufficient without the explicit definition and positioning of the components. And the code ?

#include <EEPROM.h>
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#include <Adafruit_MAX31856.h>

// Use software SPI: CS, DI, DO, CLK
Adafruit_MAX31856 maxthermo = Adafruit_MAX31856(10, 11, 12, 13);

#include <Fonts/FreeSans9pt7b.h>
#include <Fonts/FreeSans12pt7b.h>
#include <Fonts/FreeSerif12pt7b.h>
#include <FreeDefaultFonts.h>
#include <TouchScreen.h>

#define MINPRESSURE 10
#define MAXPRESSURE 1000

// Bellow 4 Pin for Touch funtionality
const int XP = 6;
const int XM = A2;
const int YP = A1;
const int YM = 7;

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
//ID=0x9341

int sysPin = 1;        // Dont change Pin number
int Buzzer = 0;         // Dont change Pin number

int X_Min = 100;
int X_Max = 910;
int Y_Min = 189;
int Y_Max = 915;

int sumTemp = 0;
int counter = 0;
int address = 1;                     //EEPROM address counter

unsigned long timer;
unsigned long int milli_time;
unsigned long previousTime = 0;

#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF
#define MAROON 0x7800
#define PURPLE 0x780F

Adafruit_GFX_Button on_btn, off_btn;

int pixel_x, pixel_y;     //Touch_getXY() updates global vars
bool Touch_getXY(void)
{
  TSPoint p = ts.getPoint();

  pinMode(YP, OUTPUT);      //restore shared pins
  pinMode(XM, OUTPUT);
  digitalWrite(YP, HIGH);   //because TFT control pins
  digitalWrite(XM, HIGH);

  bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);

  if (pressed) {
    pixel_y = map(p.x, X_Min, X_Max,  0, tft.height());       //.kbv makes sense to me
    pixel_x = map(p.y, Y_Min, Y_Max,  0, tft.width());
  }
  return pressed;
}

void writeTemp()
{

  while (digitalRead(sysPin) == LOW) {                                    
    maxthermo.triggerOneShot();
    tft.fillCircle(10, 33, 8, GREEN);                                       
    delay(100);                                                             
    tft.fillCircle(10, 33, 8, RED);                                        
    milli_time = millis();

    sumTemp = sumTemp + maxthermo.readThermocoupleTemperature();            
    counter++;

    if (counter == 10 ) {                                                   
      counter = 0;
      delay(100);

       EEPROM.write(address, (sumTemp / 10));                             // write value to current address counter address

      address += 2;                                                         // increment address counter
      if (address == 51)                               
      {
        address = 1;          // 1                                          // if yes: reset address counter

      }

      tft.setCursor(10, 170);
      tft.setTextColor(GREEN);
      tft.setTextSize(5);
      tft.print(sumTemp / 10);

      delay(10000);
      sumTemp = 0;
    }
  }
}

void callMem() {


  for (int i = 1 ; i < 11 ; i++) {      // 11

    byte value = EEPROM.read(i);                //read EEPROM data at address i
    if (value != 0)                             //skip "empty" addresses
    {

      tft.setCursor(20, 170);
      tft.setTextColor(GREEN);
      tft.setTextSize(5);
      tft.print(value);  

      delay(1500);       

      tft.fillRect(1, 130, 240, 80, RED);
    }
  }
}

void clearMem() {

  for (int i = 1 ; i < 11 ; i++) {    // 11
    if (EEPROM.read(i) != 1)                    //skip already "empty" addresses
    {
      EEPROM.write(i, 1);                       //write 0 to address i
    }
  }

  tft.setCursor(15, 140);
  tft.setTextColor(GREEN);
  tft.setTextSize(3);
  tft.print("Yes Clear");

  address = 1;                                  //reset address counter
  delay(3000);
  tft.fillRect(1, 130, 240, 80, RED);
}

void handleFault(uint8_t fault) {

  while (fault) {
    maxthermo.triggerOneShot();
    fault = maxthermo.readFault();
    delay(1);
  }

}

void btnHandle() {
  bool down = Touch_getXY();
  on_btn.press(down && on_btn.contains(pixel_x, pixel_y));
  off_btn.press(down && off_btn.contains(pixel_x, pixel_y));
  if (on_btn.justReleased())                      // Yes Button
    on_btn.drawButton();
  if (off_btn.justReleased())                     // No Button
    off_btn.drawButton();

  //************Yes Button Action**************************
  if (on_btn.justPressed()) {
    on_btn.drawButton(true);
    callMem();
  }
  //************Erase Button Action**************************
  if (off_btn.justPressed()) {
    off_btn.drawButton(true);
    clearMem();
  }
}

void setup(void)
{
  pinMode(sysPin, INPUT_PULLUP);
  pinMode(Buzzer, OUTPUT);
  digitalWrite(Buzzer, LOW);

  uint16_t ID = tft.readID();

  maxthermo.begin();

  maxthermo.setThermocoupleType(MAX31856_TCTYPE_K);

  if (ID == 0xD3D3) ID = 0x9341; // write-only shield
  tft.begin(ID);
  tft.setRotation(0);            //PORTRAIT
  tft.fillScreen(BLACK);

  delay(3000);
  tft.fillScreen(BLACK);

  on_btn.initButton(&tft,  60, 240, 100, 40, WHITE, CYAN, MAGENTA, "Yes", 2);  
  off_btn.initButton(&tft, 180, 240, 100, 40, WHITE, CYAN, MAGENTA, "No", 2);  
  on_btn.drawButton(false);
  off_btn.drawButton(false);

  tft.fillRect(1, 130, 240, 80, RED);
  timer = millis();                           //millis() returns the time since program start in ms


}

void loop(void)
{
 // batteryRead();

  maxthermo.triggerOneShot();
  uint8_t fault = maxthermo.readFault();
  if (fault) handleFault(fault);

  writeTemp();
  btnHandle();

}

PCB was developed by Professional Designer,who only provide PCB layout after statisfactory testing. What I have is PCB layout for fabrication.

With UNO, when button was pressed,temperature was displaying as per code,but with PCB when button is pressed,Atmeha328 gets RESET??????

After many request designer give me schematic....He said all remaing pins dedicated to LCD.

Your schematic and PCB layout do not match, for example:

  • On your PCB layout you have Q1, Q3 and Q4 (no Q2 for some reason), these don't exist on the schematic.
  • On your schematic you have Y1 connected to pins 10 and 18, on your PCB layout you have Y2 connected to pins 9 and 10. I don't know if there is a Y1 on your PCB because too much text is obscured.
  • On your schematic you have R3 to pin 1, on your schematic R8 is connected to pin 1.

I suspect there are other differences, but I stopped looking after that.

You cannot expect help if you don't provide accurate and consistent information.

1 Like

So, as per this code, it is not continuously monitoring your TX pin. Your void loop(void) is calling a couple of functions, and the push button position is only read when it is executing the writeTemp() function. executing this function can be pretty fast. so if you want it to register your push, you will have to keep it pressed when your is executing the writeTemp() function.

this is not ideal of course. hardware interrupts are well suited for this kind of scenarios. (only digital pin 2 and 3 support this unfortunately).

another thing, try using a debouncing capacitor with the other leg of the switch.

idk if this was of any help or not, however, best of luck

1 Like

I will try your suggestion

Well i have similar shields, and i know also A0 usually doesn't get used, but yes all digital pins except 0 & 1

int sysPin = 1;        // Dont change Pin number
int Buzzer = 0;         // Dont change Pin number

If i would use these as GPIO's i would select the RX as the input and the Tx as the output, but ok.

  for (int i = 1 ; i < 11 ; i++) {    // 11
    if (EEPROM.read(i) != 1)                    //skip already "empty" addresses
    {
      EEPROM.write(i, 1);                       //write 0 to address i
    }
  }

You should avoid EEPROM addresses 0 - 20 in combination with the MCUfriend library, don't ask me why other than that it causes issues on an AVR.

well clearly there is something not quite right, also some things in the PCB layout do not make any sense Have a look at how pin 7 is connected to pins 20 & 21. Talking about taking the long way home ! How professional is that person ?

Anyway none of this should be responsible for the issue you are encountering, so the most obvious reason would be something with the soldering. I am assuming you are using a socket, but can you post a picture of the soldering ?

1 Like

Thank You Sir, debouncing capacitor solved the problem

Exactly, Debouncing was the issue

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.