It compiles but doesn't upload

Hello, I want to try something on Arduino but I couldn't upload the code. Can you help me? I connected the Arduino UNO CH340, relay card, rc522 RFID card. I wrote the code to the Arduino IDE program. It compiles but can't upload.
Code:

#include <SPI.h>
#include <MFRC522.h>

#define SS_PIN 10
#define RST_PIN 9
#define RELAY_PIN 7

MFRC522 rfid(SS_PIN, RST_PIN);

// Buraya kendi kart UID’ini yaz (Serial Monitor'dan öğrenebilirsin)
MFRC522::Uid authorizedUID = {4, {0xDE, 0xAD, 0xBE, 0xEF}, 0};

bool relayState = false; // Röle durumu (başlangıçta kapalı)
bool cardPreviouslyRead = false; // Aynı kartı tekrar tekrar okumamak için bayrak

void setup() {
Serial.begin(9600);
SPI.begin();
rfid.PCD_Init();
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW); // Röle kapalı başlasın
Serial.println("Sistem hazır. Kart okutun...");
}

void loop() {
// Kart mevcut değilse veya okunamıyorsa çık
if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()) {
cardPreviouslyRead = false;
return;
}

// Kart zaten okunduysa tekrar işlem yapma
if (cardPreviouslyRead) return;

// Eğer yetkili kartsa, röle durumunu değiştir
if (isAuthorized(rfid.uid)) {
relayState = !relayState; // Röle durumunu tersine çevir
digitalWrite(RELAY_PIN, relayState ? HIGH : LOW);

if (relayState)
  Serial.println("Röle AÇILDI");
else
  Serial.println("Röle KAPATILDI");

} else {
Serial.println("Yetkisiz kart!");
}

cardPreviouslyRead = true; // Aynı kart için tekrar işlem yapılmasını engelle

// Kart iletişimini sonlandır
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
}

// Kart UID kontrol fonksiyonu
bool isAuthorized(MFRC522::Uid uid) {
if (uid.size != authorizedUID.size) return false;
for (byte i = 0; i < uid.size; i++) {
if (uid.uidByte[i] != authorizedUID.uidByte[i]) return false;
}
return true;
}

Error Message:
(waiting for a long time)

avrdude: stk500_initialize(): (a) protocol error, expect=0x14, resp=0x10
avrdude: initialization failed, rc=-1
Double check connections and try again, or use -F to override
this check.

avrdude: stk500_recv(): programmer is not responding
Failed uploading: uploading error: exit status 1

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

Please post your full sketch, using code tags when you do

Posting your code using code tags prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

It is also helpful to post error messages in code tags as it makes it easier to scroll through them and copy them for examination

As your topic does not relate directly to the installation or operation of the IDE it has been moved to a more relevant category of the forum

Hi @frk444 ,

this is how your code should be posted:

#include <SPI.h>
#include <MFRC522.h>

#define SS_PIN 10
#define RST_PIN 9
#define RELAY_PIN 7

MFRC522 rfid(SS_PIN, RST_PIN);

// Buraya kendi kart UID’ini yaz (Serial Monitor'dan öğrenebilirsin)
MFRC522::Uid authorizedUID = {4, {0xDE, 0xAD, 0xBE, 0xEF}, 0};

bool relayState = false; // Röle durumu (başlangıçta kapalı)
bool cardPreviouslyRead = false; // Aynı kartı tekrar tekrar okumamak için bayrak

void setup() {
  Serial.begin(9600);
  SPI.begin();
  rfid.PCD_Init();
  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, LOW); // Röle kapalı başlasın
  Serial.println("Sistem hazır. Kart okutun...");
}

void loop() {
  // Kart mevcut değilse veya okunamıyorsa çık
  if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()) {
    cardPreviouslyRead = false;
    return;
  }

  // Kart zaten okunduysa tekrar işlem yapma
  if (cardPreviouslyRead) return;

  // Eğer yetkili kartsa, röle durumunu değiştir
  if (isAuthorized(rfid.uid)) {
    relayState = !relayState; // Röle durumunu tersine çevir
    digitalWrite(RELAY_PIN, relayState ? HIGH : LOW);

    if (relayState)
      Serial.println("Röle AÇILDI");
    else
      Serial.println("Röle KAPATILDI");
  } else {
    Serial.println("Yetkisiz kart!");
  }

  cardPreviouslyRead = true; // Aynı kart için tekrar işlem yapılmasını engelle

  // Kart iletişimini sonlandır
  rfid.PICC_HaltA();
  rfid.PCD_StopCrypto1();
}

// Kart UID kontrol fonksiyonu
bool isAuthorized(MFRC522::Uid uid) {
  if (uid.size != authorizedUID.size) return false;
  for (byte i = 0; i < uid.size; i++) {
    if (uid.uidByte[i] != authorizedUID.uidByte[i]) return false;
  }
  return true;
}

You can use the same method to post error messages (especially if they are longer):

avrdude: stk500_initialize(): (a) protocol error, expect=0x14, resp=0x10
avrdude: initialization failed, rc=-1
Double check connections and try again, or use -F to override
this check.

avrdude: stk500_recv(): programmer is not responding
Failed uploading: uploading error: exit status 1

Use the < CODE> button in the post editor menu:

image

In this case your problem is not related to your code but with the upload process. For appropriate assistance you might post

  • Arduino IDE version
  • Tools setting

If you use Version 1.8.19, check the "Programmer" setup ...

Good luck!

Arduino IDE version 1.8.19
Programmer Arduino as ISP
I tried both Arduino as ISP and AVRISP mkll Nothing changed

  1. Could you upload e.g. the blink example when you got the board?
  2. Have you tried to upload with nothing connected? What was the result.
  3. Which operating system are you using?
  4. Are you uploading over USB? In that case the programmer setting is not relevant.
  5. If over USB, is the print on the CH340 readable? Or is that chip just blank?

Windows 10 pro

Blink also says it is loading and waits for a long time, then the message "An error occurred while loading the draft" appears.

I also tried the Arduino Uno board, same error.

I connect via USB.
The serial port screen does not say anything.
I think the chip is empty.

1 Like

You have been given very specific instructions, but so far have failed to follow them. Do NOT expect any help until you do.

hello, without any detailed info to give,
if you cannot upload code check the following:

  1. USB cable good and connected well ?
  2. On the tools menu the item "Port" is grey ? If it is grey, the IDE does not find a device (processor) connected. If it is black click on it and see the available USB ports. Close the menu and unplug the USB cable, then look again on the tools menu which USB Port disappeared, this one and only this one is your Arduino. Plug in the USB and select the right port.
  3. Did you select the right processor in the tools menu ? Wrong selection can lead to this strange behaviour.