<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="https://wiki.electrolab.fr/skins/common/feed.css?303"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://wiki.electrolab.fr/index.php?action=history&amp;feed=atom&amp;title=Projets%3APerso%3A2011%3AMotorshield%3Ause</id>
		<title>Projets:Perso:2011:Motorshield:use - Revision history</title>
		<link rel="self" type="application/atom+xml" href="https://wiki.electrolab.fr/index.php?action=history&amp;feed=atom&amp;title=Projets%3APerso%3A2011%3AMotorshield%3Ause"/>
		<link rel="alternate" type="text/html" href="https://wiki.electrolab.fr/index.php?title=Projets:Perso:2011:Motorshield:use&amp;action=history"/>
		<updated>2026-04-12T00:14:52Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.22.4</generator>

	<entry>
		<id>https://wiki.electrolab.fr/index.php?title=Projets:Perso:2011:Motorshield:use&amp;diff=2437&amp;oldid=prev</id>
		<title>Clément: Created page with &quot;= Motorshield use it page =  add links to get back  add a descriptive header...  == Things you should know == Remember, YMMV: this is a prototype, not a commercial product. * PWM...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.electrolab.fr/index.php?title=Projets:Perso:2011:Motorshield:use&amp;diff=2437&amp;oldid=prev"/>
				<updated>2011-12-13T22:53:59Z</updated>
		
		<summary type="html">&lt;p&gt;Created page with &amp;quot;= Motorshield use it page =  add links to get back  add a descriptive header...  == Things you should know == Remember, YMMV: this is a prototype, not a commercial product. * PWM...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;= Motorshield use it page =&lt;br /&gt;
 add links to get back&lt;br /&gt;
 add a descriptive header...&lt;br /&gt;
&lt;br /&gt;
== Things you should know ==&lt;br /&gt;
Remember, YMMV: this is a prototype, not a commercial product.&lt;br /&gt;
* PWM frequency shall be under 11 kHz according to datasheet. It can be selected when you create a ''AF_DCMotor'' object: &amp;quot;AF_DCMotor motor_1_or_2(1,MOTOR12_2KHZ);&amp;quot; or &amp;quot;AF_DCMotor motor_3_or_4(3,MOTOR34_2KHZ);&amp;quot;&lt;br /&gt;
* I think I ended up messing with signals, that is motor x forward/backward isn't 100% garanteed to be the same as for Adafruit's version. That's lame, but it seems okay to me, given that it is a prototype. One can still pilot all four DC motors, you just have to figure out which one is where and goes which way  - or wait for the final HW revision, or the specific SW library.&lt;br /&gt;
* because of previous issue, stepper support most probably won't work out of the box.&lt;br /&gt;
* speed value is messed up, see below.&lt;br /&gt;
* you can read each H-bridge status flags (see datasheet: indicates if everything's fine), if you have the right 0 ohm resistor soldered.&lt;br /&gt;
&lt;br /&gt;
== Setting speed for DC motors ==&lt;br /&gt;
The PWMing for speed is an active low on the MC33932 vs an active high on the L293D. I have been lazy to implement a HW thing to compensate for that. I suppose I'll have to decide what I end up doing: either keep the hw simple or respect full pin/sw compatibility.&lt;br /&gt;
Anyway, on this prototype, speed values are inversed ! that means, &amp;quot;motorX.setSpeed(255);&amp;quot; will stop the motor and &amp;quot;motorX.setSpeed(0);&amp;quot; will have it go fullspeed.&lt;br /&gt;
&lt;br /&gt;
== Current feedback feature ==&lt;br /&gt;
To use the current feedback feature, you should:&lt;br /&gt;
* have the right 0 ohm (eg, bridge) resistors in place (check schematic)&lt;br /&gt;
* do something like &amp;quot;analogRead(MOTORX_FBPIN)&amp;quot; with &amp;quot;const int M1_FB = A0;//M1 current feedback&amp;quot;,&amp;quot;const int M2_FB = A1;//M2 current feedback&amp;quot;,&amp;quot;const int M3_FB = A3;//M3 current feedback&amp;quot;,&amp;quot;const int M4_FB = A4;  //M4 current feedback&amp;quot;.&lt;br /&gt;
* use the datasheet, schematic and your brain to find the value ! Or read below.&lt;br /&gt;
&lt;br /&gt;
How to find the right code to have a meaningful value:&lt;br /&gt;
*The current mirror on feedback pin is 0.24% of load current.&lt;br /&gt;
*The resistor in place has a value of 270 ohm (standard precision).&lt;br /&gt;
*Therefore, a 5A current will achieve 5*0.0024*270 = 3.24 volts seen on the ADC&lt;br /&gt;
*The ADC is 10bits with a 5v reference, so this 5A current will result in 3.24/5*1024 = 664 approx.&lt;br /&gt;
*Then 5/664 (or 0.00753) is the factor to have a value in amps.&lt;br /&gt;
*An almost correct code might be something like &amp;quot;value_miliamps = 8*analogRead(MOTORX_FBPIN);&amp;quot;. Remember that the Arduino doesn't like floating point variables nor making divisions (no dedicated hw) and there are imprecisions here and there, so a x8 factor should be good enough.&lt;br /&gt;
*Note that at 6.5A the overcurrent protection kicks in, so the ADC should be fine in any case.&lt;br /&gt;
&lt;br /&gt;
== Code example ==&lt;br /&gt;
formating sucks, sorry !&lt;br /&gt;
&amp;lt;nowiki&amp;gt;&lt;br /&gt;
// Adafruit Motor shield library&lt;br /&gt;
// copyright Adafruit Industries LLC, 2009&lt;br /&gt;
// this code is public domain, enjoy!&lt;br /&gt;
// adapted for Electrolab Motorshield prototype&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;AFMotor.h&amp;gt;&lt;br /&gt;
const int STATUS_FLAG_1 = A2;//H-bridge1 status flag&lt;br /&gt;
const int STATUS_FLAG_2 = A5;//H-bridge2 status flag&lt;br /&gt;
const int M1_FB = A0;  //M1 current feedback  &lt;br /&gt;
const int M2_FB = A1;  //M2 current feedback&lt;br /&gt;
const int M3_FB = A3;  //M3 current feedback&lt;br /&gt;
const int M4_FB = A4;  //M4 current feedback&lt;br /&gt;
&lt;br /&gt;
//we need PWM freq &amp;lt; 11kHz&lt;br /&gt;
AF_DCMotor motor1(1,MOTOR12_8KHZ);&lt;br /&gt;
AF_DCMotor motor2(2,MOTOR12_8KHZ);&lt;br /&gt;
AF_DCMotor motor3(3,MOTOR34_8KHZ);&lt;br /&gt;
AF_DCMotor motor4(4,MOTOR34_8KHZ);&lt;br /&gt;
&lt;br /&gt;
void setup() {&lt;br /&gt;
  Serial.begin(9600);&lt;br /&gt;
  Serial.println(&amp;quot;Motor test! Everyone full forward !&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
  //enable status flag reading&lt;br /&gt;
  pinMode(STATUS_FLAG_1, INPUT);//Status flag1&lt;br /&gt;
  pinMode(STATUS_FLAG_2, INPUT);//Status flag2&lt;br /&gt;
&lt;br /&gt;
  // turn off (!!) all motors&lt;br /&gt;
  motor1.setSpeed(255);  &lt;br /&gt;
  motor2.setSpeed(255);  &lt;br /&gt;
  motor3.setSpeed(255);  &lt;br /&gt;
  motor4.setSpeed(255);&lt;br /&gt;
  motor1.run(RELEASE);&lt;br /&gt;
  motor2.run(RELEASE);&lt;br /&gt;
  motor3.run(RELEASE); &lt;br /&gt;
  motor4.run(RELEASE);&lt;br /&gt;
  &lt;br /&gt;
  // turn on (!!) all motors&lt;br /&gt;
  motor1.setSpeed(0);&lt;br /&gt;
  motor2.setSpeed(0);&lt;br /&gt;
  motor3.setSpeed(0);&lt;br /&gt;
  motor4.setSpeed(0);&lt;br /&gt;
  motor1.run(FORWARD);&lt;br /&gt;
  motor2.run(FORWARD);&lt;br /&gt;
  motor3.run(FORWARD);&lt;br /&gt;
  motor4.run(FORWARD);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void loop() {&lt;br /&gt;
  int tempval;&lt;br /&gt;
&lt;br /&gt;
  Serial.print(&amp;quot;motor1 current(mA):&amp;quot;);&lt;br /&gt;
  tempval = 3*analogRead(M1_FB);//check the datasheet &amp;amp; schematic, value in mA&lt;br /&gt;
  Serial.println(tempval);&lt;br /&gt;
&lt;br /&gt;
  Serial.print(&amp;quot;motor2 current(mA):&amp;quot;);&lt;br /&gt;
  tempval = 3*analogRead(M2_FB);//check the datasheet &amp;amp; schematic, value in mA&lt;br /&gt;
  Serial.println(tempval);&lt;br /&gt;
&lt;br /&gt;
  Serial.print(&amp;quot;motor3 current(mA):&amp;quot;);&lt;br /&gt;
  tempval = 3*analogRead(M3_FB);//check the datasheet &amp;amp; schematic, value in mA&lt;br /&gt;
  Serial.println(tempval);&lt;br /&gt;
&lt;br /&gt;
  Serial.print(&amp;quot;motor4 current(mA):&amp;quot;);&lt;br /&gt;
  tempval = 3*analogRead(M4_FB);//check the datasheet &amp;amp; schematic, value in mA&lt;br /&gt;
  Serial.println(tempval);&lt;br /&gt;
&lt;br /&gt;
  &lt;br /&gt;
  Serial.print(&amp;quot;status flag1:&amp;quot;);&lt;br /&gt;
  if(HIGH==digitalRead(STATUS_FLAG_1))&lt;br /&gt;
    Serial.println(&amp;quot;ok&amp;quot;);&lt;br /&gt;
  else&lt;br /&gt;
    Serial.println(&amp;quot;nok&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  Serial.print(&amp;quot;status flag2:&amp;quot;);&lt;br /&gt;
  if(HIGH==digitalRead(STATUS_FLAG_2))&lt;br /&gt;
    Serial.println(&amp;quot;ok&amp;quot;);&lt;br /&gt;
  else&lt;br /&gt;
    Serial.println(&amp;quot;nok&amp;quot;);&lt;br /&gt;
    &lt;br /&gt;
  Serial.println(&amp;quot;&amp;quot;);    &lt;br /&gt;
  delay(1000);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;/div&gt;</summary>
		<author><name>Clément</name></author>	</entry>

	</feed>