Monthly Archives: September 2019

Sequence Controller Part 3–Board design and MOSFET testing…

Boards are in the house!

IMG_9608

JLCPCB did a nice job, and the boards look fine. Except:

IMG_9609

Yeah. Those pins are beautifully aligned a very precise 0.1” from where they are supposed to be…

Pro tip: Print out your design and put your components on it so that you can check the design.

Meta pro tip: Follow your pro tips.

Anyway, that’s not the only problem; it turns out that the power and LED parts of the connector are right underneath the end of the board, so you can’t use a normal header on them (you could use a right-angle one if you wanted), so I did a new revision of the board with 1.0” rather than 1.1” for the ESP and extended the board so the connectors are out on the end. That’s on the slow ship from China right now.

Then I did a bit of bodging with some long-tail female headers so I could still do testing.

IMG_9611

Then I put a header for the LEDs and carefully soldered 8 resistors and LEDs to the output pins, so that I have an 8-channel version available for writing software.

IMG_9612

The MOSFETs are pretty darn small, but soldering them was mostly okay. I didn’t bother doing a stencil for this rev so I could reflow, but I will likely do that for the next version.

I have not yet tested what I think is the coolest part of the design; the board is both a main board and an expander board; you can connected a second version of the board on the back of the one with the ESP32 connected to it, and it will get you channels 9-16.

Here’s a quick video of the current state:



Sequence controller test from Eric Gunnerson on Vimeo.

It’s doing a “breathe” on all 8 LEDs with varying timespans for the delay action.

This is the 6th or 7th time that I’ve written sequencing software; there was a Motorola HC11 version, two AVR versions with AC dimming, a 4-channel chaser, and a couple of WS2812 versions.

They were all very simple; take the current state of all the output and drive the outputs to a new state over a given period of time. That works fine, but writing the animation can be annoying and it’s not very compact. This time I wanted to do something different and more elegant:

Here’s my spec:

     IMG_9614

That means ‘loop variable %A from 0 to 7’, and then execute a 100 cycle (1 second) dim of channel %A from its current state to 1.0 (full bright), and then do the same dim back down to zero.

I also wanted to write the vast majority of the code on my desktop, so I took a break and wrote three blog posts about how I do that. It’s basically compile-time dependency replacement with unit tests mostly written using TDD.

Then it was off to writing a *lot* of test code and a lot of classes; 18 difference source files, only two of which are ESP specific at this point. And 15 test classes to drive the tests. It mostly worked great, I did 95% of my coding and only had once latent bug that I had to track down on the ESP32. It was weird one that turned out to have very random behavior. I suspected it was uninitialized data, and that turned out to be mostly right; two subsequent calls to a method used the same stack and I forgot that strncpy doesn’t copy a null. But it all works now. Here’s the code the video is running:

$1$LOOP %B 100:10:-10
    $1$LOOP %A 0:7
        $%B$D%A,1.0
        $%B$D%A,0.0
    $1$ENDLOOP
$1$ENDLOOP”

Variable %B is used to change the cycle count for the operations from 100 to 10 in steps of 10, and then the inner loop cycles through the 8 different outputs. Everything works great.

The code all lives here if you want to see the actual code or a more realistic testing example.

Next steps:

  1. Wireless implementation to do the connection to the ESP
  2. Save and load of the animation
  3. Web interface to edit the animation.
  4. Change the language to move the cycle count into the “dim” command, as it’s not necessary for the loop commands.
  5. Build a second board to test channels 9-16.





Sequence Controller Part 3–Board design and MOSFET testing…

Boards are in the house!

IMG_9608

JLCPCB did a nice job, and the boards look fine. Except:

IMG_9609

Yeah. Those pins are beautifully aligned a very precise 0.1” from where they are supposed to be…

Pro tip: Print out your design and put your components on it so that you can check the design.

Meta pro tip: Follow your pro tips.

Anyway, that’s not the only problem; it turns out that the power and LED parts of the connector are right underneath the end of the board, so you can’t use a normal header on them (you could use a right-angle one if you wanted), so I did a new revision of the board with 1.0” rather than 1.1” for the ESP and extended the board so the connectors are out on the end. That’s on the slow ship from China right now.

Then I did a bit of bodging with some long-tail female headers so I could still do testing.

IMG_9611

Then I put a header for the LEDs and carefully soldered 8 resistors and LEDs to the output pins, so that I have an 8-channel version available for writing software.

IMG_9612

The MOSFETs are pretty darn small, but soldering them was mostly okay. I didn’t bother doing a stencil for this rev so I could reflow, but I will likely do that for the next version.

I have not yet tested what I think is the coolest part of the design; the board is both a main board and an expander board; you can connected a second version of the board on the back of the one with the ESP32 connected to it, and it will get you channels 9-16.

Here’s a quick video of the current state:



Sequence controller test from Eric Gunnerson on Vimeo.

It’s doing a “breathe” on all 8 LEDs with varying timespans for the delay action.

This is the 6th or 7th time that I’ve written sequencing software; there was a Motorola HC11 version, two AVR versions with AC dimming, a 4-channel chaser, and a couple of WS2812 versions.

They were all very simple; take the current state of all the output and drive the outputs to a new state over a given period of time. That works fine, but writing the animation can be annoying and it’s not very compact. This time I wanted to do something different and more elegant:

Here’s my spec:

     IMG_9614

That means ‘loop variable %A from 0 to 7’, and then execute a 100 cycle (1 second) dim of channel %A from its current state to 1.0 (full bright), and then do the same dim back down to zero.

I also wanted to write the vast majority of the code on my desktop, so I took a break and wrote three blog posts about how I do that. It’s basically compile-time dependency replacement with unit tests mostly written using TDD.

Then it was off to writing a *lot* of test code and a lot of classes; 18 difference source files, only two of which are ESP specific at this point. And 15 test classes to drive the tests. It mostly worked great, I did 95% of my coding and only had once latent bug that I had to track down on the ESP32. It was weird one that turned out to have very random behavior. I suspected it was uninitialized data, and that turned out to be mostly right; two subsequent calls to a method used the same stack and I forgot that strncpy doesn’t copy a null. But it all works now. Here’s the code the video is running:

$1$LOOP %B 100:10:-10
    $1$LOOP %A 0:7
        $%B$D%A,1.0
        $%B$D%A,0.0
    $1$ENDLOOP
$1$ENDLOOP”

Variable %B is used to change the cycle count for the operations from 100 to 10 in steps of 10, and then the inner loop cycles through the 8 different outputs. Everything works great.

The code all lives here if you want to see the actual code or a more realistic testing example.

Next steps:

  1. Wireless implementation to do the connection to the ESP
  2. Save and load of the animation
  3. Web interface to edit the animation.
  4. Change the language to move the cycle count into the “dim” command, as it’s not necessary for the loop commands.
  5. Build a second board to test channels 9-16.





Just enough biochemistry – Insulin resistance and Gluconeogenesis

Disclaimer: “Just enough biochemistry” means I’m going to be simplifying what is some very complex biochemistry. Sometimes the omitted details are going to matter.

****

There’s this weird thing that happens as part of insulin resistance that I don’t think gets discussed often enough, and since I think it drives much of what is bad about insulin resistance, I’m going to discuss it here. We’ll start with glucose metabolism:

Elevated blood glucose / elevated insulin

When blood glucose is elevated, the pancreas secretes insulin, and that changes how the body’s metabolism operates:

image

Insulin is a signal to pull excess glucose out of the blood. The body has 4 ways to do this, shown as purple arrows in the diagram. The are, from the right to the left:

  1. Store it as muscle glycogen. There are a lot of muscle cells in the body and the conversion to glycogen is quick, so they can take up a lot of glucose quickly – that is is why the arrow is so big. But their ability to take up glucose is dependent on how much glycogen they already are storing – how much space there is for new glycogen. There be a lot of extra space after a long run and very little space after a few hours watching TV.
  2. Store it as liver glycogen. The liver can’t store quite as much glycogen, but it can still store it pretty quickly.
  3. Burn it as it comes into the bloodstream. The size of this arrow depends on what you are doing; if you are just sitting it’s pretty small, if you are exercising it can be bigger. With high insulin the body will choose to burn more glucose and less fat; notice the small size of the fat arrow.
  4. Convert it to fat. Excess glucose that can’t be handled in the other ways will be converted to fat and stored through a process known as “de novo lipogenesis” (“new fat creation”). The body isn’t very fast at doing this, so this arrow is small. If there is a lot of glucose to convert to fat, this process will take hours.

Broadly speaking, this part of the diagram is about storing glucose energy that is coming in from the diet.

Low blood glucose / low insulin

When blood glucose is low, the pancreas secretes glucagon:

image

When the blood glucose is low, the glucagon is a signal to do something to increase the glucose in the blood.

There are only two sources – outside of eating – to raise blood glucose when it is low:

  1. The glycogen that was stored in the liver previously can be converted back to glucose and released into the bloodstream. This is good for the short term, on the order of a few hours, but at some point the liver will run out of glycogen.
  2. If glycogen stores run low, then the body will convert metabolic leftovers into glucose through a process known as “gluconeogenesis” (new glucose creation). This might be temporary until more carbs are eaten, or it might be continuous if there is little glucose coming through food.

When glucose is rare, the body will try to conserve it, so it will burn proportionally more at; that is why the fat arrow is bigger in this part of the diagram.

There is another process – ketosis – that kicks in when there is little glucose coming in from the diet, but that’s a bit beyond the scope of this discussion.

This part of the picture is about running the body on energy that has been previously stored.

The full picture

Here’s the full picture:

image

This diagram shows how a healthy – insulin sensitive – metabolism works. If you eat a meal that has enough carbs to raise your blood glucose, you will be in the top half of the diagram. Between meals – and especially at night – you will be on the bottom portion of the diagram.

Blood glucose and insulin resistance

One of the tests for insulin resistance/type II diabetes is the Oral Glucose Tolerance Test. In the OGTT a person who has fasted overnight drinks a solution with 50 grams of glucose in it and their glucose level is measured every 30 minutes.

image

This chart shows data from an OGTT for three different people.

Because these people have fasted overnight, we would expect them to have used liver glycogen to keep their blood glucose up for most of the night, and that would leave them with some room to store most of the glucose from the test. If that were true, the glucose could be stored quickly without a lot of blood glucose change and a couple of hours later things would be back to normal. And that is what we see on the “normal chart”.

The discussion about the other lines would typically focus on how much higher the blood glucose levels are and how much longer they take to get back to the starting point, but I want to focus on another factor. Note that the other lines start at significantly elevated blood glucose levels. I find this puzzling; let me explain why…

Looking at the patient with moderate type II diabetes, we see that in a span of 90 minutes, they got rid of a lot of blood glucose, from 280 mg/dl to 170 mg/dl, for a difference of 110 mg/dl. Or something like 70 mg/dl in an hour.

But overnight, they can’t get their blood glucose below 130 mg/dl, despite not eating anything and having 8-12 hours in which it could happen. We would expect that the natural use of glucose would drop the blood glucose to normal levels by morning; in fact, it would drop it too much if the liver didn’t add glucose.

Where is the glucose coming that is keeping that from happening?

That is the puzzle.

Looking back at the diagram, there are only two possible sources for the glucose. Maybe it could come from the glycogen stores, but if that were the case we would expect them to be more depleted in the morning and therefore be able to handle the 50 grams of glucose from the test. That doesn’t appear to be the case.

Which points the finger very strongly at gluconeogenesis.

Gluconeogenesis malfunction

In the diagram, I showed gluconeogenesis on the bottom; it is only active when blood glucose was low and glycogen stores are low.

Unfortunately, when somebody becomes insulin resistance, something gets broken with the regulation of gluconeogenesis. Normally it gets turned off when there is insulin in the system, but that gets broken when there is an accumulation of fat in the liver.

The result is a constant supply of glucose into the bloodstream, with the amount related to how much fat has accumulated in the liver.

What does this mean metabolically?

A constant stream of unexpected glucose means that between meals and overnight, we are depleting our glycogen reserves less. That is the big effect we are seeing in the charts; there is less free room in the glycogen store, so more of the glucose that we eat needs to be converted to fat. That not only means more fat in our fat stores, it takes longer to do this conversion, so our insulin levels are elevated for longer. And any time that our insulin levels are elevated is time when we’re on the upper half of the diagram.. So, more fat stored, less time to burn fat.

Not good. But initially the body is still able to deal with the amount of carbohydrate in the diet and the amount being generated and still keep fasting blood glucose levels normal. It does require more insulin all the time – taking the first step towards hyperinsulinemia – and it will also push up HbA1c levels. That’s why HbA1c is a better diagnostic test for insulin resistance for *most* people than fasting blood glucose. Fasting insulin level is probably better still but is not widely used.

As insulin resistance gets worse, there is more glucose created, enough so the glycogen stores are full most of the time.  Any glucose that can’t be burned off immediately goes straight to fat, and the high insulin percentage – the percentage of time in a day when insulin is elevated – goes up. Hyperinsulinemia gets worse, carbs that are eaten go mostly to fat, and fat eaten goes there as well.

Eventually, the pancreas loses the capacity to produce enough insulin and/or the fat and muscle cells get poor enough at pulling glucose out of the bloodstream, the fasting glucose level goes up, and it’s type II diabetes.

The big point here is that a person who is insulin resistant has different metabolism than one who is not; it’s a bit like they are eating a small amount of candy around the clock.

Diabetes is a chronic disease

For a long time, the ADA recommended high-carb, low fat diets as the only diet for type II diabetes. For people who wanted to lose weight – which is true for most type II diabetics – the recommendation would include a small calorie deficit of around 300-500 calories per day.

What is the impact of that diet for somebody who is insulin resistant and has hyperinsulinemia? We still have the glucose created by the liver and we still have a lot of glucose coming in from the diet, so we’re still going to have the hyperglycemia. As long as the hyperglycemia is there, the insulin is there – we’re on the top half of the diagram – it’s hard to burn fat. So, the body has a few options:

  • Turn down our basal metabolic rate to burn fewer calories.
  • Turn down our optional metabolic activity to burn fewer calories.
  • Convert some protein to energy
  • Try to get us to eat more
  • So, we get cold, tired, hungry, and lose lean muscle mass.

    But we still have the hyperglycemia, insulin resistance, and blood glucose issues.

    This is why type II diabetes is generally considered to be a chronic disease. It’s pretty well known that if people can manage to lose weight, their diabetes symptoms get better, but the prescribed diets are generally ineffective at producing a permanent change. I think that the arrow of causation runs the opposite direction; if you can manage to improve your hyperinsulinemia, you will lose weight.

    Dealing with hyperinsulinemia

    The hyperinsulinemia is at the root of the problem. How can we get the insulin down?

    There’s really only one solution; we need to get the carbohydrates down enough so that the excess glucose produced by the liver is no longer enough to cause significant hyperinsulinemia. If that excess glucose becomes metabolically desirable – necessary to run our bodies – than it will no longer be problematic.

    We need to get back to the bottom part of the diagram. There are a couple of different ways of doing this.

    We could just reduce the amount we eat by a lot. Enough so that the glucose we are eating plus the extra from gluconeogenesis is being put to use to run the body and we don’t need any insulin to store it. We could do this by going on a very low calorie diet, something under 800 calories per day. We could get a gastric bypass, which has a similar effect (gastric bypass also appears to have some additional effect because of the physical changes made). Both gastric bypass and very low calorie diets have good clinical evidence for reversing insulin resistance and getting rid of the symptoms of type II diabetes. Some of the fasting protocols will also likely work, though we don’t have the same quality of studies to support them.

    Or we could just try to reduce the amount of carbs we eat by a lot. So low that we would expect to be relying on gluconeogenesis to produce the glucose that we need and low enough that we don’t have a lot of glucose to process. In other words, a keto or zerocarb diet. And keto diets also have good levels of clinical evidence achieving the result we want.

    With other diets, people get less diabetic and perhaps lose some weight, but still end up diabetic at the end. My assumption is that they don’t do as well because those diets don’t deal with hyperinsulinemia *unless* you convert them to very-low-calorie diets.

    Notes

    As noted, this is a really complex area. For basic biochemistry, there are a number of good references out there; I like “Marks Basic Medical Biochemistry”, which can be found in PDF form online.

    For more of the details, see the following links:

    Insulin resistance and gluconeogenesis

  • Insulin regulation of gluconeogenesis
  • Unraveling the Paradox of Selective Insulin Resistance in the Liver: the Brain–Liver Connection
  • Resolving the Paradox of Hepatic Insulin Resistance
  • Keto diets

  • Virta Health Research Page
  • Virta Spreadsheet of low-carb studies
  • Very-low-calorie diets

  • Primary care-led weight management for remission of type 2 diabetes (DiRECT): an open-label, cluster-randomised trial
  • Very Low-Calorie Diet and 6 Months of Weight Stability in Type 2 Diabetes: Pathophysiological Changes in Responders and Nonresponders
  • Calorie restriction for long-term remission of type 2 diabetes
  • Gastric bypass

    Bariatric Surgery A Systematic Review and Meta-analysis

    Hyperglycemia

    Fasting Insulin vs Hemoglobin A1c: Are We Getting It Right?

    Effect of Physiological Hyperinsulinemia on Gluconeogenesis in Nondiabetic Subjects and in Type 2 Diabetic Patients

    Diabetes and diet

    The Dilemma of Weight Loss in Diabetes