PIC16F877A with DC Gear Motor

Home Forums Microcontrollers PIC Microcontroller PIC16F877A with DC Gear Motor

Tagged: ,

Viewing 15 posts - 1 through 15 (of 26 total)
  • Author
    Posts
  • #11449
    Shengo
    Participant

    Hi there, I have read through your tutorial about interfacing DC Motor with PIC Microcontroller using IC L293D and also how to enable the output by controlling the input of PIC with the use of LED light, the part I don’t understand is what is a pull up resistor you meant in the tutorial? The pull up resistor is connected to the switch.

    I am currently doing a project which is a robot, when I turn on the switch, the robot will move forward direction, when it touches the wall, a sensor will be triggered and the motor will turn in another direction to move backwards. Any idea how the connection should be?

    One last question, the code you posted is in what language please? Is it in C++ or?

    Your help is very much appreciated, thanks.

    Below is the tutorial links that I have question about:

    https://electrosome.com/dc-motor-l293d-pic-microcontroller/

    https://electrosome.com/push-button-switch-pic-microcontroller/

     

    #11452
    Ligo George
    Keymaster

    Hi,

    About PULL UP or PULL DOWN resistors used along with Switches.

    Switch is connected to an input pin of Microcontroller. A pin is in input state means that it is in HIGH IMPEDANCE state. So if you don’t connect any PULL UP or PULL DOWN resistors that pin will remains FLOATING when the switch is in OFF state. A floating pin may catch noises and reading such a pin will result in unexpected results. Its value usually varies erratically varies.

    Programming Language

    It is C.

    #11453
    Shengo
    Participant

    Can you show me how should I connect my circuit and an example of code with 2 input to control 2 output should be please? Thanks

    I know those 2 outputs should be connected to my L293d, just confused how the input should be connected and how the code should be, cause once if I turn on the switch, it will send signal to input 1 and the motor turn forward (clockwise direction), but what if I need to send another signal from sensor 1 to input 2 to turn the motor anti clockwise? Will those 2 signal overlap? By overlapping I mean both input is high.

    Thanks very much.

    #11457
    Ligo George
    Keymaster

    Just read the above tutorials carefully, you can do it yourself. You can connect two switches to any of the IO pins of PIC Microcontroller. You can make those pins Input by writing to TRIS register. Similarly you can connect L293D inputs to 2 outputs of MCU and make those pins Output by writing to TRIS register.

    #11460
    Shengo
    Participant

    Hi there, below is the code I tried to write according to your tutorials:

    void main()
    {
      TRISD.F0 = 1; //RD0 as Input PIN 1
      TRISD.F1 = 1; //RD1 as Input PIN 2
    
      TRISB.F0 = 0; //RB0 as Output PIN 1
      TRISB.F1 = 0; //RB1 as Output PIN 2
    
      PORTB.F0 = 0; //Motor clockwise direction flow OFF
      PORTB.F1 = 0; //Motor anticlockwise direction flow OFF
    
      do
      {
        if(PORTD.F0 == 0) //If Switch is presseed
        {
          delay_ms(100); //Provides required delay
          if(PORTD.F0 == 0) //If Switch is still pressed
          {
            PORTB.F0 = 1; //Motor turns in clockwise direction
            if(PORTD.F1 == 1) //If Signal is sent from proximity sensor
            {
              PORTB.F0 = 0;
              PORTB.F1 = 1;
              delay_ms(10000);
              PORTB.F1 = 0; //After 10 seconds the motor is turned off
            }
          }
        }
      }while(1);
    }

    The part where the proximity sensor sends a signal, if(PORTD.F1 == 1), the input should be 1 right?

    Also, do you need a constant flow of current from input, in order for the output to be able to work constantly? Like if I press the switch once, the motor turns clockwise, but if I cut off the current from the switch, will the motor still keep turning or it will stop??

    Furthermore, will this code works in MPLAB or Hi-tech? I know the code is almost similar with just a slight modification, just need to know about this, if I want to program them in other software, anything else to add to the code?

    One more question, the voltage of the signal sent from my proximity sensor is quite high, which my PIC16F877A couldn’t stand the voltage as the maximum is 5V, so any solution to this to lower down the signal voltage from my proximity sensor? I’ve searched the internet, I found something about opto-isolator, can I use that?

    Your help is greatly appreciated as this is a very urgent project, thanks.

    #11466
    Ligo George
    Keymaster

    Firstly, above code is for MikroC Pro compiler. You can reduce the voltage of Proximity Sensor using Voltage dividing resistors and/or zener diode.

    Continuous pressing of switch is not required, it depends on how you program (how many times you read switch) etc.

    #11468
    Shengo
    Participant

    Could you please check if the code I posted is sensible and correct?

    My circuit working principle is like this,

    Press switch, RD0 become zero and sends a signal to RB0 to become 1, then RB0 sends signal to my L293D input 1 to turn the motor clockwise. Then, when the sensor is triggered, RD1 will be 1, thus it sends signal to RB1 to become 1, which RB1 sends to input 2 of L293D to turn the motor anti clockwise. Then after 10 seconds, the motor is stopped.

    Is the coding above correctly matched with what I have describe?

    Furthermore, is there any other H bridge circuit IC you can introduce to me that can provide output current up to 1.5A?

    Thanks for taking your time reading this.

    #11469
    Ligo George
    Keymaster

    It seems like the the program is correct as per the logic explained by you. You can use L298N, if you need more current.

    #11481
    Shengo
    Participant

    Hi there, sorry to disturb you again. I have tested the above code with my PIC16F877A, when I have input for RD0, LED 1 turns on (I replace motor with LED first for testing). Then when I have input for RD1, the LED 1 turns off then LED 2 turns on for 5 seconds and turn off, but once it has turned off, LED 2 lights up again, then after 5 seconds it turns off again and this loop keeps on repeating.

     

    I wonder if those code you posted in your website is all on looping? If I just want all the sequence above just to happen once, and then when I press the push button again, the sequence happens again, I don’t want them to be repeating by themselves, shall I just remove the part “do while”?

    Or should I use any “return” command for my code in order for them to return to all original state after the sequence finish?

    Thank you for your advice. Your quick reply is very much appreciated.

    #11484
    Ligo George
    Keymaster

    Are you sure that you are using push switch ?

    If you are using push switch, the sequence will not repeat without pressing of switch.

    Most of the microcontroller programs requires infinite loop as it needs to continuously read sensor status, switch status etc.

    #11485
    Shengo
    Participant

    No, sorry. Because I am just using wire connect directly to both RD0 and RD1 to test my circuit, when I connect RD0 to ground, LED1 lights up, when I connect RD1 to +5V, LED1 turns off then LED 2 lights up for 5 seconds then turn off.

     

    The thing is the repeating part is not the “if” part at the push button, it is the second “if” part of the code, which is the 5 seconds turn on then off the LED2. Must I use a push button to test the circuit? Because I’m planning to use a sensor for RD1 though, only push button for RD0.

     

    Or is there any problem with my code?

     

    Thanks.

    #11486
    Ligo George
    Keymaster

    As long as RD0 = 0 and RD1 = 1 the sequence will repeat continuously as per the above program.

    #11487
    Shengo
    Participant

    Okay, I finally get the program now. Tried it with push button today.

     

    Can you please suggest a way to make the program only loop by one time? Afterwards if I press the push switch again then the program run again, I just want it to only run once, each time is only start by myself but not automatically by the infinite loop.

     

    Your help is really important and appreciated. Thanks.

    #11488
    Ligo George
    Keymaster

    Sorry, I don’t understand what you are telling. The above program will check the sensor input only when you press the push button switch. It will not give any output without pressing of switch.

    #11490
    Shengo
    Participant

    What I meant was, I just want the whole program just to run once, what’s happening in the program now is that after the 5 seconds delay and RB1 becomes low, and then the program itself will go back to the initial part again, which is detect the push switch status (by this time it will turn on the RB1 because I haven’t turn off my switch), and turn on RB0 again. The program keeps repeating and repeating the whole sequence by itself.

     

    Any way just to only let it run only once, without repeating itself? After each time it finishes running, everything will just switch off without running back to the first part of the program again. And when I press the push switch again, the program will run again just ONCE. I don’t want it to be keep running in an infinite loop.

     

    I hope I explain myself well this time, thanks.

Viewing 15 posts - 1 through 15 (of 26 total)
  • You must be logged in to reply to this topic.
>