{"id":1418,"date":"2015-09-06T21:15:55","date_gmt":"2015-09-07T07:15:55","guid":{"rendered":"http:\/\/blog.familjenjonsson.org\/blog\/?p=1418"},"modified":"2017-02-25T18:34:41","modified_gmt":"2017-02-26T04:34:41","slug":"microsquirting-the-nc30-part-9-some-debugging","status":"publish","type":"post","link":"https:\/\/blog.familjenjonsson.org\/blog\/2015\/09\/06\/microsquirting-the-nc30-part-9-some-debugging\/","title":{"rendered":"Microsquirting the NC30, part #9: Some debugging"},"content":{"rendered":"<p>Finally, it was time to go for a test ride. You can view and log data over Bluetooth with a phone using the MSdroid Android app. Going out for a ride, everything seemed good until the RPM gauge spiked to 10k and then back down. Uh oh &#8212; this is what the Atmega does when it boots, so this was an indication that it had rebooted &#8220;in flight&#8221;. That&#8217;s not supposed to happen. A few moments later, the RPM gauge started going totally nuts, bouncing between 10k and max, down to zero, jumping around randomly, and then dying.<\/p>\n<p>This is the most frustrating part about programming an Arduino &#8212; you can&#8217;t just run it in the debugger and see what&#8217;s happening. I commented out various parts of the code to see if I could isolate what was causing it, but it seemed semi-random. By going over the code with a fine-toothed comb, sure enough, I found one instance of a forgotten &#8220;&amp;&#8221;, hence using the value of a variable, rather than its address, as a pointer. I also found another place with flawed logic that could lead to writing to random places in memory. But even after fixing this, the problems remained. I didn&#8217;t get the random behavior of the RPM gauge anymore, but it would still reboot.<\/p>\n<p>Looking at the logs, it was interesting to note that sometimes the Bluetooth connection to the Microsquirt was interrupted at the same time the . That&#8217;s a totally separate circuit, the only common part between the bluetooth modem and the microcontroller is &#8230; the power supply!<\/p>\n<p>This shouldn&#8217;t be a big surprise, actually, automotive power is notoriously noisy, and this is an area that I don&#8217;t know that much about. If the power circuitry didn&#8217;t properly insulate the microcontroller from various transients, that could easily be causing the problems. To test this theory, I went for a spin with the alternator unplugged. Sure enough, the problem went away.<\/p>\n<p>Ok, time to rip up the electronics box again. I&#8217;m getting quite skilled at it, though.<\/p>\n<p>The conversion from 12V to 5V is done by a tiny MIC5205 voltage regulator on the circuit board OshPark made for me. It&#8217;s specced for 150mA, and the Atmega, CAN-bus controller, transceiver and the Bluetooth modem all use no more than 70mA. Sounds fine, right? Not quite. The maximum current is one limit, but it turns out that most often, the max current output is limited by the maximum power dissipation.<\/p>\n<p>Linear voltage regulators change an input voltage to a lower output voltage by essentially converting the excess power to heat. This means that the regulator will dissipate a power equal to the current passing through it times the voltage drop, V_in-V_out. The limiting factor is the maximum temperature of the silicon junction, so how much power can be passed through it depends on how efficiently heat can be transported away. (This is why it helps to put heat sinks on stuff&#8230;)<\/p>\n<p>Well, it turns out that using the thermal resistance quoted by the datasheet, when supplying 70mA the regulator will overheat when the output-input voltage difference is maybe 3-4V (depending on the ambient temperature). That would be an input voltage of 8-9V, when in reality it is being fed 12V with the engine off and up to 15V when running! There&#8217;s no way it can handle that much power, and when it overheats, it will go into thermal shutdown. No wonder I&#8217;ve been having problems!<\/p>\n<p>So, I pulled the circuit board out and thought about what to do. The obvious solution is to add a pre-regulator that will take the 12-15V down to &lt;8V that the MIC5205 can handle. I\u00a0happen to have a box of LM317 regulators. Those are big (TO-220 case) and can handle a lot more power. The only hitch is that they are adjustable and require some extra components,\u00a0so it wasn&#8217;t so easy to add it onto the existing circuit, but it worked out. Here&#8217;s what I ended up with:<\/p>\n<div id=\"attachment_1423\" style=\"width: 610px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/blog.familjenjonsson.org\/blog\/wp-content\/uploads\/2015\/09\/IMG_20150906_082435.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1423\" class=\"size-medium-size wp-image-1423\" src=\"http:\/\/blog.familjenjonsson.org\/blog\/wp-content\/uploads\/2015\/09\/IMG_20150906_082435-600x431.jpg\" alt=\"This is the newly added LM317 voltage regulator, sitting between the headers that the circuit board with the CAN controller fits into. It's almost half the size of that entire board... The resistor soldered across the pins in part of the voltage dicider that sets the output voltage. Also added was the 47uF capacitor immediately to the left.\" width=\"600\" height=\"431\" srcset=\"https:\/\/blog.familjenjonsson.org\/blog\/wp-content\/uploads\/2015\/09\/IMG_20150906_082435-600x431.jpg 600w, https:\/\/blog.familjenjonsson.org\/blog\/wp-content\/uploads\/2015\/09\/IMG_20150906_082435-150x108.jpg 150w, https:\/\/blog.familjenjonsson.org\/blog\/wp-content\/uploads\/2015\/09\/IMG_20150906_082435-300x215.jpg 300w, https:\/\/blog.familjenjonsson.org\/blog\/wp-content\/uploads\/2015\/09\/IMG_20150906_082435.jpg 1000w, https:\/\/blog.familjenjonsson.org\/blog\/wp-content\/uploads\/2015\/09\/IMG_20150906_082435-730x524.jpg 730w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><p id=\"caption-attachment-1423\" class=\"wp-caption-text\">This is the newly added LM317 voltage regulator, sitting between the headers that the circuit board with the CAN controller fits into. It&#8217;s almost half the size of that entire board&#8230; The resistor soldered across the pins in part of the voltage divider that sets the output voltage. Also added was the 47uF capacitor immediately to the left.<\/p><\/div>\n<p>As you can see, the LM317 ended up fitting nicely under the CAN controller circuit board. If you look hard, you can see the 12V supply trace on the back side has been cut, and routed through the input and output pins on the LM317 (on the right and center, respectively.)<\/p>\n<div id=\"attachment_1424\" style=\"width: 610px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/blog.familjenjonsson.org\/blog\/wp-content\/uploads\/2015\/09\/IMG_20150906_082422.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1424\" class=\"size-medium-size wp-image-1424\" src=\"http:\/\/blog.familjenjonsson.org\/blog\/wp-content\/uploads\/2015\/09\/IMG_20150906_082422-600x326.jpg\" alt=\"Here's where it gets ugly. A bunch of components were added to the back side of the board.\" width=\"600\" height=\"326\" srcset=\"https:\/\/blog.familjenjonsson.org\/blog\/wp-content\/uploads\/2015\/09\/IMG_20150906_082422-600x326.jpg 600w, https:\/\/blog.familjenjonsson.org\/blog\/wp-content\/uploads\/2015\/09\/IMG_20150906_082422-150x81.jpg 150w, https:\/\/blog.familjenjonsson.org\/blog\/wp-content\/uploads\/2015\/09\/IMG_20150906_082422-300x163.jpg 300w, https:\/\/blog.familjenjonsson.org\/blog\/wp-content\/uploads\/2015\/09\/IMG_20150906_082422.jpg 1000w, https:\/\/blog.familjenjonsson.org\/blog\/wp-content\/uploads\/2015\/09\/IMG_20150906_082422-730x396.jpg 730w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><p id=\"caption-attachment-1424\" class=\"wp-caption-text\">Here&#8217;s where it gets ugly. A bunch of components were added to the back side of the board.<\/p><\/div>\n<p>To set the output voltage, you need two resistors. The first is soldered between the output and adjustment pins right next to the case. The second is on the back of the board and goes to ground (the 2.7k resistor on the right in the picture above.) The tan square is the 4.7uF capacitor for the LM317 regulator input.<\/p>\n<p>While I was adding stuff, I decided to add a diode (the small black component) that prevents this circuit from being pulled down if there is a transient low-voltage event. The glass component is a 20V zener diode that protects against transient overvoltages (and reverse voltages) by shunting those to ground, and the 10 Ohm resistor will limit the current that needs to be shunted in those cases. It also forms an RC low-pass filter\u00a0with the capacitor, but its time constant is 50us so it will only help against high-frequency noise.<\/p>\n<p>On the LM317 output, I added a bigger 47uF capacitor, which in principle will be able to supply the 70mA current for half a millisecond.<\/p>\n<p>While it&#8217;s certainly not improving the increasingly ghetto-looking circuit board, it actually worked pretty OK to add the components across the traces. Now I&#8217;m happy I made those traces as wide as I did!<\/p>\n<p>Testing the new circuit, the MIC5205 seems a lot happier. When I ran it on 12V before, you&#8217;d burn your finger touching it. Now it&#8217;s not noticeably hot, and the LM317 was warm-ish to the touch, even with 15V input. Seems pretty promising.<\/p>\n<p>After mounting all the circuit boards into the box again (did I mention I&#8217;m getting good at it?), I went for another test drive.<\/p>\n<p>Flawless!<\/p>\n<div id=\"attachment_1421\" style=\"width: 716px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/blog.familjenjonsson.org\/blog\/wp-content\/uploads\/2015\/09\/lambdamap.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1421\" class=\"size-full wp-image-1421\" src=\"http:\/\/blog.familjenjonsson.org\/blog\/wp-content\/uploads\/2015\/09\/lambdamap.png\" alt=\"This is an example of what the data look like. The axes are manifold pressure and lambda, with RPM being the color scale. As suspected, the bike runs really rich, typically around lambda 0.8, and it even saturates the wideband sensor at 0.69 lambda a significant part of the time at full throttle.\" width=\"706\" height=\"580\" srcset=\"https:\/\/blog.familjenjonsson.org\/blog\/wp-content\/uploads\/2015\/09\/lambdamap.png 706w, https:\/\/blog.familjenjonsson.org\/blog\/wp-content\/uploads\/2015\/09\/lambdamap-150x123.png 150w, https:\/\/blog.familjenjonsson.org\/blog\/wp-content\/uploads\/2015\/09\/lambdamap-300x246.png 300w, https:\/\/blog.familjenjonsson.org\/blog\/wp-content\/uploads\/2015\/09\/lambdamap-600x493.png 600w\" sizes=\"auto, (max-width: 706px) 100vw, 706px\" \/><\/a><p id=\"caption-attachment-1421\" class=\"wp-caption-text\">This is an example of what the data look like. The axes are manifold pressure and lambda, with RPM being the color scale. As suspected, the bike runs really rich, typically around lambda 0.8, and it even saturates the wideband sensor at 0.69 lambda a significant part of the time at full throttle.<\/p><\/div>\n<p>Over 20 minutes, there was not a single hickup. Clearly that meant problem solved, so\u00a0I immediately went for a ride up the Saddle Road all the way up to the Mauna Kea visitor station at 9200 feet. (I wanted to get data on how what the air\/fuel ratios with the carburetors were all the way up there before starting to change things.) Above is an excerpt of the logged data.<\/p>\n<p>As expected, the bike is running rich. Very rich. The plot above is near sea level and shows lambda (air\/fuel ratio in relation to the ideal 14.7) against manifold pressure, essentially throttle opening. It appears to run at about lambda 0.8 for partial throttle, and the full-throttle lambda goes all the way down to 0.69 which is as rich as the SLC lambda controller reports!<\/p>\n<p>As you might expect, this got worse at altitude. (Carburetors meter fuel against air volume, not mass, so as\u00a0air density decreases, they run richer and richer.) Going up the final 2000ft, the lambdas were often pegged at the 0.69 limit. The bike runs really poorly at that altitude, and I guess that&#8217;s not a surprise.<\/p>\n<p>For reference,\u00a0maximum power for a gasoline engine occurs at about a lambda of 0.86. At full throttle, you might need to go a bit richer than that, maybe to 0.82, to prevent the engine from knocking. But running at 0.7 would not only make much worse power, it would also make the exhaust really sooty (check) and smelly from vastly increased hydrocarbon emissions (check). Not to mention waste gas. The two cylinder pairs also run at noticeably different lambdas. I think there is\u00a0plenty of room for improvement here! \ud83d\ude09<\/p>\n<p>Now that I know the sensors are working, the next step is to\u00a0pull out the stock ignition module and get the Microsquirt to handle the ignition.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Finally, it was time to go for a test ride. You can view and log data over Bluetooth with a phone using the MSdroid Android app. Going out for a ride, everything seemed good until the RPM gauge spiked to 10k and then back down. Uh oh &#8212; this is what the Atmega does when &#8230;<\/p>\n<p><a href=\"https:\/\/blog.familjenjonsson.org\/blog\/2015\/09\/06\/microsquirting-the-nc30-part-9-some-debugging\/\" class=\"more-link\">Continue reading &lsquo;Microsquirting the NC30, part #9: Some debugging&rsquo; &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[4,29],"tags":[11,13,15],"class_list":["post-1418","post","type-post","status-publish","format-standard","hentry","category-motorcycle","category-nc30","tag-electronics","tag-motorcycles","tag-nc30"],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/blog.familjenjonsson.org\/blog\/wp-json\/wp\/v2\/posts\/1418","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.familjenjonsson.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.familjenjonsson.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.familjenjonsson.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.familjenjonsson.org\/blog\/wp-json\/wp\/v2\/comments?post=1418"}],"version-history":[{"count":6,"href":"https:\/\/blog.familjenjonsson.org\/blog\/wp-json\/wp\/v2\/posts\/1418\/revisions"}],"predecessor-version":[{"id":1428,"href":"https:\/\/blog.familjenjonsson.org\/blog\/wp-json\/wp\/v2\/posts\/1418\/revisions\/1428"}],"wp:attachment":[{"href":"https:\/\/blog.familjenjonsson.org\/blog\/wp-json\/wp\/v2\/media?parent=1418"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.familjenjonsson.org\/blog\/wp-json\/wp\/v2\/categories?post=1418"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.familjenjonsson.org\/blog\/wp-json\/wp\/v2\/tags?post=1418"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}