Home › Forums › Microcontrollers › PIC Microcontroller › PIC16F877A with DC Gear Motor
- This topic has 25 replies, 2 voices, and was last updated 9 years, 4 months ago by Ligo George.
-
AuthorPosts
-
June 4, 2015 at 2:55 pm #11449ShengoParticipant
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/
June 4, 2015 at 8:58 pm #11452Ligo GeorgeKeymasterHi,
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.
June 5, 2015 at 5:56 am #11453ShengoParticipantCan 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.
June 6, 2015 at 10:07 am #11457Ligo GeorgeKeymasterJust 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.
June 9, 2015 at 3:40 pm #11460ShengoParticipantHi 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.
June 10, 2015 at 8:43 am #11466Ligo GeorgeKeymasterFirstly, 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.
June 10, 2015 at 5:11 pm #11468ShengoParticipantCould 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.
June 11, 2015 at 8:23 am #11469Ligo GeorgeKeymasterIt seems like the the program is correct as per the logic explained by you. You can use L298N, if you need more current.
June 16, 2015 at 2:22 pm #11481ShengoParticipantHi 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.
June 16, 2015 at 9:13 pm #11484Ligo GeorgeKeymasterAre 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.
June 17, 2015 at 5:44 am #11485ShengoParticipantNo, 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.
June 17, 2015 at 8:19 am #11486Ligo GeorgeKeymasterAs long as RD0 = 0 and RD1 = 1 the sequence will repeat continuously as per the above program.
June 17, 2015 at 3:35 pm #11487ShengoParticipantOkay, 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.
June 17, 2015 at 9:04 pm #11488Ligo GeorgeKeymasterSorry, 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.
June 18, 2015 at 6:14 am #11490ShengoParticipantWhat 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.
-
AuthorPosts
- You must be logged in to reply to this topic.