Go Down

Topic: L298N - PWM control - analogWrite values below 255 not recognised (Read 259 times) previous topic - next topic


Hi All,

Take 2 at a post - the last one crashed on upload.

I'm teaching mechatronics to a class, and am using a simple autonomous robot as one of the introductory learning activities, as per the hundreds of online tutorials, eg:

The robot detects obstacles using the ultrasonic sensor and then looks left and right, driving off in the direction of furthest distance in front of it.  

I'm a process engineer in a past life, and have refined the code so it works perfectly for this task. However, I'm trying to extend the task for the few students who: "just get it" while the rest are working out the basics.

I want to set up this 'robot' to slow down when it is within 500mm of an obstacle, gradually slowing down to a crawl as it approaches it.  I envisaged that this would be a simple map of the PWM speed to distance,
eg:  int LHMotorSpeed = map(distance,50,1,255,60);  

I am working ahead of the class and have encountered a programming flaw I can't see that has stopped this from working.  

All help gratefully appreciated.

Facts in dot point below.

*jumpers removed off enA and enB pins on the L298N bridge
*Connections, enA=9, enB=10 (PWM-enabled pins)
*in1, in2, in3, in4 to pins 4-7 respectively

*motors turn if analogWrite(enX,255) used in code
*motors do not turn if speed values of 254 or below used in code.

* code downloaded from the internet to check operation of L298 board works perfectly - connections fine

* have tried the following:
- directly forcing datatype to int through: int MotorSpeed = map(...).  Did not work
- casting datatype: MotorSpeed = (int) map...   Did not work
- creating a new int variable and assigning the value to the new variable.  Did not work
- typing in "254" at the same spot where "255" allowed the motors to turn.  Motors did not turn
- giving up in frustration and looking at it with a new pair of eyes/objectively.

It's almost as though my code is assigning a boolean value of on or off, but I couldn't see how.

* Test code works and appears identical.

Could anyone comment on possible problems here please - I'm at a wits end with this one, and have exhausted the options I could think of.

Code that works = L298_Motor_Test_PWM
My code that doesn't = Autonomous_Cr_final_9_Jul


I'm guessing that you aren't using a Mega and you didn't read the documentation for the Servo library. See Servo Reference The second paragraph includes:
"On boards other than the Mega, use of the library disables analogWrite() (PWM) functionality on pins 9 and 10, whether or not there is a Servo on those pins."

Try some different PWM pins.




Outstanding - thanks - skim read but the devil was in the detail... Hopefully the title will pick up others' searches with the similar mistake.

I left the board/model at work - will try it out first thing tomorrow.


Go Up