The Atmega328 Standalone is not working on serial pins 2 RX and 3 TX

Dear Colleagues

I need your help to get out of this problem.


I am making a sensor that works with an ATMega328p to measure the voltage of a panel and send the data wirelessly via an XBee, to a computer.

I'm already in the testing phase and I just need to integrate the programming in the microcontroller to be able to test the whole sensor mounted on a breadboard.

How does the programming or sketch work?

This programming is made so that it is in an infinite loop until a signal is sent from the computer. Once this signal is received by the XBee, the system goes out of the loop and starts sending data periodically.

The problem:

When sending the signal from the computer to the sensor it does not react.

What have I done?

Using Digi's XCTU software, for the Xbee, I found out that I can do an antenna scan and detect the surrounding antennas that belong to the same channel and ID. And the antennas can communicate with each other, which leads me to believe that it is not an antenna failure.



#include <SD.h>
#include <SPI.h>
#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>
#include <SafeString.h>
#include <SoftwareSerial.h>

//--------------------------------------INICIALIZACION DE VARIABLES------------------------------

const int chipSelect = 4;

const int ADCpin = A0;
float voltPanel;
String sfVolts = "";

SoftwareSerial XBeeSerial(2, 3); //(Rx, TX)
uint8_t Rx_nextByte;

//--------------------------------------ENCABEZADO DE TRAMA DE ENVIO------------------------------

byte packet1[] = {0x7E, 0x00, 0x27}; //Byte delimitador y Longitud de trama // variar por el voltaje
byte packet2[] = {0x10, 0x01, 0x00, 0x13, 0xA2, 0x00, 0x40, 0xD7, 0xAE, 0xAD, 0xFF, 0xFE, 0x00, 0x00,}; //Id de trama, tipo de trama, direccion de 64bits, opciones y numero de brincos.
// xx.xx,00:11:46,05/01/2021 == 25 chars
byte fullPacket[43];// (Delimitador + longitud )+ (tipo + direccion)+ voltage + fecha + checksum = 37
//  [3 + 14 + 4 + 19 + 1]
char buffer [27]; // 19 la fecha  +1  '\0', 25 bytes de tamaño // cambiar porque hay voltaje
#define DEBUG SafeString::Output

void setup() {
  Serial.println("CODIGO PRINCIPAL");



void loop() {

//--------------------------------------LOOP MIENTRAS ESPERA EL MENSAJE DE ARRANQUE------------------------------
  Serial.println(F("Espera a recibir señal"));
  while (XBeeSerial.available()) {
    Rx_nextByte =;
    if (Rx_nextByte == START_DELIMITER) {
      //Inicia a mandar datos Si el primer Byte que recive de la PC es un 0x7E.  
      Serial.println("Señal recibida");

//--------------------------------------SE INICIA TOMANDO LA HORA FECHA ------------------------------    

      tmElements_t tm;
      if ( {
        sprintf(buffer, "%02d:%02d:%02d,%02d/%02d/%04d", tm.Hour , tm.Minute, tm.Second, tm.Day, tm.Month, tmYearToCalendar(tm.Year));
        Serial.println(buffer); // Compruebo la fecha y hora actual.
//--------------------------------------SE COMIENZA A FORMAR Y CONTAR LOS CARACTERES DEL ENCABEZADO ------------------------------  
        size_t idx = 0;
        memmove(fullPacket + idx, packet1, sizeof(packet1));
        idx += sizeof(packet1);
        memmove(fullPacket + idx, packet2, sizeof(packet2));
        idx += sizeof(packet2);
        if ((idx + 6 + 19 + 1) > sizeof(fullPacket)) {
          Serial.println(F("1 Paquete completo esta mal."));

//--------------------------------------SE TOMAN 20 MUESTRAS DEL VOLTAJE DEL SENSOR EN EL ADC ------------------------------  
        sfVolts = ""; // Voltage xx.xx == 5 y coma 1 == 6     
        const int SAMPLES = 20;
        float fv = (float(analogRead(ADCpin)) * 5.0 / 1023) * 6 + 10;
        fv = 0;
        for (int i = 0; i < SAMPLES; i++) {
          fv = fv + ((float(analogRead(ADCpin)) * 5.0 / 1023) * 6 + 10);
        fv = fv / SAMPLES;
        sfVolts = String(fv)+',';

//--------------------------------------SE EMPAQUETA EL DATO DEL ADC EN LA TRAMA------------------------------          
        memmove(fullPacket + idx, sfVolts.c_str(), 6); // Siempre van a ser 6 caracteres de largo
        idx += 6;
        if ((idx + 19 + 1) > sizeof(fullPacket)) {
            Serial.println(F("2 Paquete completo esta mal."));
//--------------------------------------SE EMPAQUETA EL DATO DEL RTC EN LA TRAMA------------------------------          
        memmove(fullPacket + idx, buffer, 19);
        idx += 19;
        //Dejo un ultimo byte para tener el Checksum.
        if ((idx + 1) != sizeof(fullPacket)) {
            Serial.println(F("3 Paquete completo esta mal."));
//--------------------------------------SE DA EL FORMATO A LA TRAMA D DATOS Y SUS CARACTERES SE CONVIERTEN A HEX------------------------------          
        int chksum = 0;
        for (size_t i = 3; i < idx; i++) { // comienza despues del tamaño dela trama
          chksum += fullPacket[i];
        chksum = (0xff & chksum);
        chksum = (0xff) & (0xff - chksum);
        fullPacket[idx] = chksum;
        idx++; // El tamao total de la trama
        if (idx != sizeof(fullPacket)) {
          Serial.println(F("fullPacket size error"));
        size_t printSize = idx * 3; // ..<space>
         XBeeSerial.write (fullPacket , idx);
         Serial.println();//Se comprueba que el formato de la Trama API.
//--------------------------------------SE GUARDAN LOS DATOS EL EL DATALOGGER------------------------------          
        String dataString = "";
        dataString += String(sfVolts);
        dataString += String(buffer);
        File dataFile ="TensionDePanel.txt", FILE_WRITE);
        if (dataFile) {
//--------------------------------------SE ENVIA LA TRAMA------------------------------          
        //XBeeSerial.write (fullPacket[i], 3);//Se envia la trama del sensor a la PC.

//--------------------------------------FUNCION QUE DA FORMATO A LA HORA Y FECHA------------------------------          
void print2digits(int number) {
  if (number >= 0 && number < 10) {

You haven’t shown how you have connected it all up but I guess you have understood that Rx and Tx are crossed over between devices.


Xbee have 4 pins GND (blue), 5V(red), Dout(green), Din(yellow)

And do the two test, TX - Din & RX - Dout and TX - Dout & RX - Din.
I dont have any results.

And I tested If the program really upload in the ATmega328 and yes because, befores this I uploading a Blinking sketch and works.

From the picture, it appears that pins D2 and D3 of the MCU chip are not connected. This conflicts with this:

It looks like you are using the wrong serial connections.

I doubt if all those libraries will fit into a ATmega328P.

1 Like

They will:

You could try this function for runtime free memory:

1 Like

Its working good my boy

Thanks I going to check