Project starts whithout button pressed

Hello everybody,

About a year ago I built a small project. It works, but sometimes behaves a bit weird. So I'm hoping someone here has an idea what's wrong.

The project it a printer, that prints a random ticket when a button is pressed.

The problem: the printer prints sometimes on it's own. It it strange, because the text is random, which means the whole process is started, as if someone pressed the button...

Hardware used:
Arduino Pro Micro
Thermal Receipt Printer Guts ([Thermal Receipt Printer Guts : ID 2753 : $49.95 : Adafruit Industries, Unique & fun DIY electronics and kits]
HW-125 SD-card module
And some parts for the power supply. You can chose between 2 power sources (battery and 230V), however, this works fine so far.

Function: When the button is pressed, the arduino accesses the SD-card, counts the lines, generates a random number, seeks the line on the SD-card accordingly and prints it.

Has anybody an idea what the cause for this bug could be?
Thank you for your help!

Here is how I wired it, I hope one can follow it:

Code:

#include <SPI.h>
#include <SD.h>
#include "Adafruit_Thermal.h"
#include "SoftwareSerial.h"

#define TX_PIN 6 // Arduino transmit  YELLOW WIRE  labeled RX on printer
#define RX_PIN 5 // Arduino receive   GREEN WIRE   labeled TX on printer

SoftwareSerial mySerial(RX_PIN, TX_PIN); // Declare SoftwareSerial obj first
Adafruit_Thermal printer(&mySerial);     // Pass addr to printer constructor

String Text="default";
String fileName="print.txt";
File myFile;
char cha;
int zaehlerzeile;
int zaehlersuche;
int gefunden=0;
int loop1=0;
int linescounted=0;
int ziel;
int maxzeile;
int max;


void setup() {
  pinMode(2, INPUT);
  pinMode(3, OUTPUT);
  digitalWrite(3,HIGH);
  //Serial.begin(9600);
  mySerial.begin(9600);
  //Printer setup
  printer.begin();
  printer.setLineHeight(50);
}

void loop (){
  //Pressing button on pin 2 starts process
  if(digitalRead(2)){
    //Serial.println("button pressed");
    delay(1000);
    initializeCard();
    //read lines on SD card
    countlines();   
    randomSeed(micros());
    max=zaehlerzeile+1;     
    //generate random number
    zaehlersuche=random(1, max);   
    //seek and save line
    seekline();  
    //print
    printer.wake();
    printing();   
    Text="";
  }
  
}
void initializeCard(){
  if(!SD.begin(4)){
    while(1);
  }
}

void countlines(){
  myFile=SD.open(fileName);
  if(myFile){
    zaehlerzeile=0;
    while(myFile.available()){
        cha=myFile.read();
        if(cha=='\n'){
            zaehlerzeile++;
          }
      }
    zaehlerzeile++;
  }
  myFile.close();
  maxzeile=zaehlerzeile;
}

void seekline(){
  gefunden=0;    
  myFile=SD.open(fileName);
  if(myFile){
    zaehlerzeile=1;
    while(myFile.available()){
      if(zaehlersuche==1){
        read();
      }
      while(gefunden==0){
        cha=myFile.read();
        if(cha=='\n'){
          zaehlerzeile++;
          if(zaehlerzeile==zaehlersuche){
            read();            
          }
        }
      }
    }
  }
}
  
void read(){
  Text=myFile.readStringUntil('\n');
  gefunden=1;
  myFile.close();
}

void printing(){
  printer.justify('C');
  printer.setSize('L');
  printer.setLineHeight(50);
  printer.println("");
  printer.println("");
  printer.println("");
  printer.println(Text);
  printer.println("");
  printer.println("");
  printer.println("");
  printer.println("");
  //delay(2000);
}

230V would burn the Arduino in a second!

Your diagram shows a 5V source. Is that what you meant?

Your switch to choose the power source is connected to Vcc. Is that the 5V pin or the Vin pin?

If it is the 5V pin, then connecting a 6V battery to it would damage the arduino.

If it is the Vin pin, then both the 6V battery and the 5V supply are too low, you need 6.5V at least.

Is there a common ground between the 5V supply and the rest of the circuit? It will not work otherwise.

Sorry, of course I meant 230V through a 5V power supply.

The Arduino gets powered through the Vcc (as labeled on the board), and it works fine with 5V. As shown in the drawing, they're all connected to a common ground. However, this is not the problem.

That is not shown in the drawing, which is why I asked about it! :wink:

But it won't work fine with 6V.

  • Show us good images of your ‘actual’ wiring.

Why are you using software serial? Pro Micro has an available hardware serial port. Unlike Uno, Nano etc, the RX/TX pins are not used for uploading & serial monitor. That hardware serial port is available to use, and if there is a hardware serial port available, it should always be used in preference to software serial.

Apologies if our comments don't seem relevant to your immediate problem, but we are reviewing the code & schematic you posted, and if we don't mention something important, we might forget.

I kept as close as possible to the example code provided by adafruit. I myself have no deeper understanding of these things. On a side note, prototyping was done on a arduino uno, and only when it worked I installed it on the micro pro. That's also the reason why in my drawing the ports for both boards are labeled.

I will then change this connection to the "raw" pin.
And a picture of my actual wiring isn't possible, as everythink is built into a very small space in a 3d printed part. It looks like a bomb, you wouldn't see a thing in there.

The Vin or Raw pin requires at least 7 volts for the Arduino on board 5v regulator to function.

To solve this, I could connect my power supply which is fixed to 5V to the Vcc and use 6 AA batteries (9V) on the raw. Then everything is within specs?

I have re-checked your code and schematic. I can't see a reason why pin 2 would read high unless the button was pressed. So I think the problem could be faulty wiring, and the pin is floating.

Are you certain that the pulldown resistor is 220R? Could it in fact be 220K or 2M2? Such high values could leave the pin vulnerable to interference which could cause "phantom" button presses.

You could try removing the resistor and connecting the button from pin 2 to ground and use INPUT_PULLUP mode.

Yes, that's possible.

Will your circuit run on 3xAA non-rechargeable batteries? This is below 5V, but might still work if connected to the Vcc pin.

Another possibility is 4xAA NiMH batteries connected to the Vcc pin. In theory this could be above 5.5V which could damage the Arduino, but when I have tested this it has never been above that. It may depend on the charger you use to charge the cells.

I checked the resistor and it is 220k. Big mistake there from me. I swapped it with a 330, as I couldn't find a 220. (This should also work is think?)

I haven't used the INPUT-PULLUP so far and I think I will leave this as it is.

For the power supply, I decided to do a redesign and use a 9V power supply and 9V batterie. It works, but due to bad soldering i fried the arduino. I also forgot about the SD-card reader, which must not exceed 5.5V, so I will include a step down converter.

For now, I can't test wether the resistor fixed the problem. So I will come back to this when I got to change everything.

The exact value of pull-up and pull-down resistors is not critical. A normal/typical value would be 4K7 or 10K. 220R or 330R is a little low, but will work. 220K is too high, as you seem to have found.

I hope you mean 6xAA, not a rectangular PP3 size 9V battery.

But even with 6xAA batteries, almost half the battery capacity will be wasted.

For battery powered circuits, I prefer to use 3.3V components and a low-drop-out 3.3V regulator. Then, a li-po battery pack or 3x or 4x NiMH AA or AAA batteries can be used. (I never normally use non-rechargeable batteries these days.)

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