Breadboarded ATMega328P

Help!!!

I have a project I’ve been tinkering with and can’t figure out what I’ve done wrong. I have an Arduino UNO that I have used to control a bluetooth-connected rover. I decided to save my Arduino for prototyping other projects and instead breadboard it out with just the ATMega328p chip. The sketch runs flawlessly when the chip is in the Arduino. However when I put the same chip in the breadboard, I get nothing. I verified the wiring is correct based on the instructions here, and even went so far as to upload the “blink” sketch to the chip and run it from the breadboard. That sketch works perfectly, no hiccups whatsoever. Unfortunately the sketch that runs fine from the Arduino does nothing when I run it on the same chip on the breadboard. I have pictures attached. The posts seen are where I have the output coming from when I run the sketch - I removed those wires to make it easier to see the connections.
Again, the blink sketch works fine on the breadboard, but I get nothing with this sketch. With the blink sketch, when I use the “reset” button, the LED on 13 flashes twice quickly before starting the sketch. With the sketch that runs the rover, I get nothing when I reset it.

Is there something I’m missing that I need to add to the code to get this to work? The code I’m using is here:

int motor1Pin1 = 11; // pin 2 on L293D IC
int motor1Pin2 = 10; // pin 7 on L293D IC
int motor2Pin1 = 6; // pin 10 on L293D IC
int motor2Pin2 = 5; // pin 15 on L293D IC


int right = 5; //turet right
int left = 4; // turet left
int up = 3; //turet up down
int fire = 2; // fire
int state;

void setup() {
    // sets the pins as outputs:
    pinMode(motor1Pin1, OUTPUT);
    pinMode(motor1Pin2, OUTPUT);
    //pinMode(enable1Pin, OUTPUT);
    pinMode(motor2Pin1, OUTPUT);
    pinMode(motor2Pin2, OUTPUT);
   pinMode(up, OUTPUT);
   pinMode(fire, OUTPUT);
    pinMode(right, OUTPUT);
    pinMode(left, OUTPUT);   


    Serial.begin(9600);
}

void loop() {
    //if some date is sent, reads it and saves in state
    if(Serial.available() > 0){     
      state = Serial.read();   
    }  
    // forward
    if (state == 'w') {
        digitalWrite(motor1Pin1, HIGH);
        digitalWrite(motor1Pin2, LOW); 
        digitalWrite(motor2Pin1, LOW);
        digitalWrite(motor2Pin2, HIGH);
    }
       // left
    else if (state == 'e') {
        digitalWrite(motor1Pin1, HIGH); 
        digitalWrite(motor1Pin2, LOW); 
        digitalWrite(motor2Pin1, HIGH);
        digitalWrite(motor2Pin2, LOW);
    }
    // right
    else if (state == 'q') {
        digitalWrite(motor1Pin1, LOW); 
        digitalWrite(motor1Pin2, HIGH); 
        digitalWrite(motor2Pin1, LOW);
        digitalWrite(motor2Pin2, HIGH);
    }
    // up left
    else if (state == 'a') {
        digitalWrite(motor1Pin1, HIGH); 
        digitalWrite(motor1Pin2, LOW); 
        digitalWrite(motor2Pin1, LOW);
        digitalWrite(motor2Pin2, LOW);
    }
    // up right
    else if (state == 's') {
        digitalWrite(motor1Pin1, LOW); 
        digitalWrite(motor1Pin2, LOW); 
        digitalWrite(motor2Pin1, LOW);
        digitalWrite(motor2Pin2, HIGH);
    }
    // down left
    else if (state == 'z') {
        digitalWrite(motor1Pin1, LOW); 
        digitalWrite(motor1Pin2, HIGH); 
        digitalWrite(motor2Pin1, LOW);
        digitalWrite(motor2Pin2, LOW);
    }
    // down right
    else if (state == 'c') {
        digitalWrite(motor1Pin1, LOW); 
        digitalWrite(motor1Pin2, LOW); 
        digitalWrite(motor2Pin1, HIGH);
        digitalWrite(motor2Pin2, LOW);
    }
    // backward
    else if (state == 'd') {
      
        digitalWrite(motor1Pin1, LOW); 
        digitalWrite(motor1Pin2, HIGH);
        digitalWrite(motor2Pin1, HIGH);
        digitalWrite(motor2Pin2, LOW);
    }
  // turet right
    else if (state == 'o') {
        digitalWrite(right, HIGH); 
        digitalWrite(left, LOW);
    }
       // turet left
    else if (state == 'p') {
        digitalWrite(right, LOW); 
        digitalWrite(left, HIGH);
    }
       // turet up down
    else if (state == 'k') {
        digitalWrite(up, HIGH); 
    }
       // fire
    else if (state == 'f') {
        digitalWrite(fire, HIGH); 
    }

     // Stop
    else  {
        digitalWrite(motor1Pin1, LOW); 
        digitalWrite(motor1Pin2, LOW); 
        digitalWrite(motor2Pin1, LOW);
        digitalWrite(motor2Pin2, LOW);
        digitalWrite(up, LOW);
        digitalWrite(fire, LOW);
        digitalWrite(right, LOW); 
        digitalWrite(left, LOW);
        noTone(13);
    }
delay(100);
}

More pictures attached for reference.

You forgot the 0.1uf decoupling caps. I'm amazed you can upload blink to it like that, honestly.

As well as any sort of power supply filtering, period, which you should probably have.

For power in to the breadboard, I'm using the power out from my motor driver. It has a voltage regulator built in to give a 5V out. I added a 10uF electrolytic cap across the power rail anyway just in case and still get the same results.

I'm a noob here to this - what is up with 0.1uF decoupling caps?

(EDIT:
I see what you mean, and yes I'm using a 10uF cap for decoupling - I already have a fixed 5v coming in so I see no need to attempt to use a 7805 voltage regulator...)

pholt_pharmd:
I'm a noob here to this - what is up with 0.1uF decoupling caps?

(EDIT:
I see what you mean, and yes I'm using a 10uF cap for decoupling - I already have a fixed 5v coming in so I see no need to attempt to use a 7805 voltage regulator...)

No, 0.1uF decoupling caps should be on every Vcc pin of the Atmega (or at least 1 per side, as close to the chip as possible).

If you have a regulated 5V coming in, yes no need for a 7805 or other regulator on your board, but a ~10uF cap across the power line coming in is still recommended (sounds like you did try this). I wouldn't tap power to run the Atmega from a power source running motors personally though.

It sounds like your code is uploading, so most likely a code bug, but do make the hardware changes suggested anyway, for stability.

As for your code, nothing looks excessively wrong, but:

  • since "state" is holding a single character, it should be of type "char", not "int".
  • give "state" an initial value, either when you declare it, or in setup().
  • You could also condense all those if-then-else statements to a single switch-case (easier to read and debug imo), but that's up to you, it should work fine either way.

I see a commented-out command referring to an enable pin. It depends on your motor driver/shield, but I highly suspect you need to uncomment that and enable your motors.

Make sure your breadboard atmega328p pinout is correct for the digital pins.
See attach.

arduino_atmega328.pdf (102 KB)

pholt_pharmd:
For power in to the breadboard, I'm using the power out from my motor driver. It has a voltage regulator built in to give a 5V out. I added a 10uF electrolytic cap across the power rail anyway just in case and still get the same results.

I'm a noob here to this - what is up with 0.1uF decoupling caps?

(EDIT:
I see what you mean, and yes I'm using a 10uF cap for decoupling - I already have a fixed 5v coming in so I see no need to attempt to use a 7805 voltage regulator...)

10uf electrolytic cap is not decoupling, it's power supply filtering. Neither a 10uf cap (of any type) nor an electrolytic cap (of any value) are suitable for decoupling.

You need 0.1uf ceramic caps between vcc and ground right next to the chip. Without them, the chip will not be stable; the current drawn by an ic can change very rapidly, such that the momentary voltage changes (due to the inductance of the power wires going to the chip) will disrupt proper functioning. This should be done for all ic's, and if you look at the datasheets, you'll see that they all specify it somewhere.

Nowhere there does it have any mention of using caps for decoupling, but I did it anyway and still nothing.
Other ideas? Anyone? Something other than implying I'm a moron for not using decoupling capacitors?

So the blink sketch works but the more complex motor sketch does not?

All I can think of is that the motor itself is interfering with the ATMEGA uC. It happens.

What I would do is run the motor sketch without the motors attached and see, with a logic probe or oscilloscope, if the outputs from the uC to the motor driver or whatever are as expected. If that runs with the motors detached and not with them attached then there is some inductive kickback that is killing the uC. If that is the case, start adding protection - isolators, kickback diodes, full Arduino body condom. :slight_smile:

If it does not run at all I would start simplifying the sketch until it works and then adding back code to where it breaks in order to gather more information on what is going wrong.

Yes, as was said before, if blink works, you have something else wrong. Read my prior post for a few code things to check on/improve, in addition to doing the motor-less test JoeN mentions which will show if there is a hardware issue. Make sure you are enabling your h-bridge! Either via pullup resistors on the 2 enable pins, or via an output from the Arduino to those same pins (which you appear to have at one time been doing).

pholt_pharmd:
Other ideas? Anyone? Something other than implying I'm a moron for not using decoupling capacitors?

Breadboards can completely stop a crystal from oscillating! Even a scope probecan kill the oscillator. Try:
this non-invasive idea.

I use breadboards but they can be a PITA...
Soldering sometimes is a better approach.

Ray

You sure you have Reset pulled high? Looks like it goes to the Gnd buss.

CrossRoads:
You sure you have Reset pulled high? Looks like it goes to the Gnd buss.

Well that’s part of the problem. Will follow up with more as I troubleshoot.

Good catch Crossroads. Also, Aref should be connected to ground through a series 0.1uF cap, not tied directly to Vcc (so leg 1 of the cap to Aref, leg 2 to Gnd, no other connections). Plus the decoupling caps mentioned previously (which are now implemented I assume).

How the heck did you load a sketch with reset held low? I'm impressed!

Can you connect 20 pico-farad capacitors between Oscillator's pins and gnd?

Programming:
Can you connect 20 pico-farad capacitors between Oscillator's pins and gnd?

He has 22pF caps there already, look at the pictures..........

So… for those interested, I figured it out I think.

Everything works fine for the chip when its in my official Arduino UNO R3 board
When I took that same chip and put it on a breadboard, NOTHING.
When I took that same chip and put it in my clone (SainSmart UNO R3) NOTHING.

I added this one line of code:

pinMode(0, INPUT_PULLUP);

and… viola…

works fine…