Posts Tagged ‘entrepreneur’

h1

In which our intrepid heroine learns about resistor dividers

September 5, 2013

I need to monitor my gadget’s battery.

It has a 4.2V LiPo battery with Adafruit’s USB charging circuit. The system can be charging and running at the same time. However, the circuit does not have a fuel gauge or other monitoring information. It has a couple LEDs to say when charging is finished and when it is charging, but nothing to say “battery is nearly dead” (what I need).

My Electric Imp can only take up to 3.3V as an input. If I try to plug by fully charged battery (4.2V) into my Imp’s ADC input, well, fire is fun but bad. (Ok, it probably won’t cause a fire but it may cause an early demise to my Imp.)

Level shifting a signal is a common thing to do. I know this because, in role as software engineer, I have said, “Can’t the hardware just level shift it to a better range for the software?” It is easy!

When someone else does it.

Swotting up

Faced with the problem, I went off to read a little bit. I asked my EE friends to recommend books (other than the physically heavy, mentally heavy, doesn’t-come-in-Kindle Art of Electronics). I read Make:Electronics awhile back and enjoyed it but it was too simple. I need something beyond “here’s how to read a schematic” and more on the order of “here’s how to solve the 25 most common problems a DIYer will have”. Though if they ever do a Headfirst Electrical Engineering book, I’d be all over that.

Anyway, I eventually found that I want a resistor divider. This is like page 3 of a good electronics book so it wasn’t like I spent more than an hour on it. I don’t remember which web page made me think, “This is what I need” since I found it in a few places, including Wikipedia. Here’s what I learned about a resistor divider:

 

So if Vin is my battery and Vout is going to go to the Imp’s ADC to be measured, I need to set the resistor values (Z1 and Z2) to be something that makes sense.

I’m too lazy to do real math when I’m just poking at this. Ideally, I’d look at the range I want to measure (4.2-2.8V) and the range I want it to shifted to (3.3-0V) and crank. But lazy. So let’s just set Z1=Z2 so my resistor divider divides everything in half. I can prototype that and not blow the Imp, work out the details later.

But wait! Resistors to ground are power vampires. Since this is a battery system, I want to minimize power.  I typed “power calculator” into Google and came up with a reasonable webpage that would do the math for me, though the equations were there so I went ahead and thought about it. Here is the important equation, from Wikipedia:

So if I want to minimize power… then I need to maximize resistance (R).

I know I’ve used these equations before but I’ve always been answering a review or exam question, not my own need-to-know musings.

Next, I thought about looking through my bag of resistors. But I realized that the 4.2V already gets translated to 3.3V so why didn’t I use that? I don’t even need to dig through a pile of anything.

Use existing resources

I connected 3.3V to my ADC input and wrote the software to sample the pin. Well, really I just copied it from the Electric Imp Sampler API page and then got it working for my system.

The ADC measured 0xFFFF (maximum value). That made sense, it can measure 0-3.3V and I was putting in 3.3V.  I sampled every second as my battery took forever to die, eventually it measured 0xFFEF. Since the ADC is only 12 bits (shifted over to 16), that actually means there was only one bit of difference.

One minute after it went from 0xFFFF to 0xFFEF, it died. So there is only one bit of difference between “great” and “dying”. Plus, only one minute of “dying”.

This method won’t work.

Resistor networks, Part 2

Well, I had estimated the resistor math so after my failure, I got my bag o’ resistors and fished out the highest value ones: 91MOhm, 33MOhm, 10MOhm, 1.5MOhm. I’ve got several of each, from buying a pack of 500 at RadioShack.

I was pulling over my solderless breadboard when I realized that this was too easy. I started hearing that “monsters ahead” music in my head. (Dear brain, can’t we find another way to communicate? And what does the Gilligan’s Island theme mean?)

Plus, it was about lunch so I shot off a quick email to Phil (of Weekend Engineering) and told him my plan. His reply was two paragraphs long. The first one made me wish we’d used a white board:

The voltage divider output is just R2 / (R2 + R1).  So if you want your peak 4.2V input to be 3.3V at the output, you want a ratio of 3.3 / 4.2 = R2 / (R2 + R1), so you want resistors proportional to R2 = 3.3, R1 = 0.9.  This makes sense intuitively if you think of a series stack of identical resistors with the voltage dropping across it.  If you had a stack of 100 equal 1 ohm resistors, then 1% of the voltage drops as you go through each resistor.  If you put 4.2V in the top, the voltage decreases by .042V every resistor you move down.  22 resistors down, the voltage has dropped by 22 x 0.042V = 0.924V to 3.276V.  So R1=22 ohms (the 22 one ohm resistors in series), and R2 = 78 ohms (the remaining 78 one ohm resistors in series.)  There’s your 0.9 : 3.3 ratio.

He probably did all that math. None of my “well, voltage squared is around ten” ballpark nonsense for him. So, cool, he did the math. I can just use his answer.

The second paragraph burst my bubble:

Now, the larger resistors you use, the less current you’re drawing through this stack of resistors.  BUT, if you draw current out of the stack, into your ADC, then the you change the voltage, because the resistor divider assumes that there are no other current paths.  So the max size resistor you can use depends upon the effective input impedance of your ADC.  You want that impedance to be very large (10X or more, possibly much more) compared to the total resistance of your resistor divider.  As the current through your resistor divider gets very small (because the resistors are huge), that signal will also be more and more subject to sources of noise. That’s the motivation for not maximizing.  (Otherwise, you’d want to just use giga-ohm scale resistances.)

Yeah… I think that was the monster: why doesn’t everyone use giant resistors? And how am I to wade through Electric Imps documentation to find what the input impedance is. (I like my Imp but their Wiki is often a maze of twisty little passages, all alike.)

I looked at their forums (mental note: write a blog post about why I deeply dislike forums) and found an answer deep in a comment thread: around 50kOhm. Later in the thread, they report which processor the Imp uses (but I don’t think they want to bandy that about so I’ll let you look). I went to the processor’s datasheet (already on my harddrive as I used it in a client project recently) and 50k looks right.

So according to Phil, my resistance needs to be 1/10 of the input impedance: around 5k. But the power dissipation is huge! (3.528mW, Phil and I exchanged emails and, again, he did the math.) Essentially, it burns 0.8mA all the time.

This is a crime in a battery powered system.

To deter my continued questions, Phil suggested using a FET to control whether the battery monitor was on. But that requires an IO pin. I don’t have any extra, I’ve already made my tricolor LED a bicolor LED since I need to have a pin from the accelerometer to wake up the Imp.

Stomp stomp grrr. Electrical engineering is all about making difficult things more difficult. Hmph.

Buy my way out

Once I got over my post-lunch-need-a-nap-fussiness, I thought about some projects I’ve worked on and tried to remember how the problem was solved (without cheating and looking at client schematics). I remember someone swearing at a fuel gauge driver and an EE remarking at how stupidly expensive the part was. A little bit of searching led me to a SparkFun board with an I2C power monitor.

The first product comment is a plea to add this to a USB LiPo charger board. That was two years ago. Nothing. Not on AdaFruit or MakerShed either. Someone is missing a big opportunity here.

Anyway, this bumps my materials cost up by $10 but solves the problem very neatly. Plus, since it is I2C, I actually get an IO pin back so I can have a tricolor LED again.

Tempting, so tempting.

Plan

If this was a startup, I would be strongly in favor of buy instead of make. You can always make (and make cheaply) later but you can’t get the startup rolling if you take on too much at the start.

I think I’ll sleep on it and see if I can find a better solution. One that doesn’t involve much soldering.

Sadly, one of the negatives is that I need to buy a few other pieces of gear and right now I’ve got carts on SparkFun, AdaFruit, and Digikey but I don’t want to have three orders of stuff. I don’t know why. I’m just that way.

 

h1

A promise that, in the future, it will be better

September 4, 2013

Elizabeth came on my podcast to talk about Dolores, an older neighbor who lives alone. We got to talking about a device that would let Dolores maintain her independence while reassuring her family (and Elizabeth) that she is fine.

The idea is similar to the thinking-of-you jar I’ve been working on to explore the Electric Imp. Essentially, add an accelerometer, put it all into something cute (i.e. a teddy bear), have Dolores pat it when she wakes up (or if it is in the kitchen, when she walks by). If Dolores doesn’t pat it by 10am or three times a day or (setting to be determined), it will contact her family who can call and checkup on her.

This isn’t a hard concept. And I’ve been scooped a bit by Goodnight Lamp. (Hmph! People who take my ideas and go back in time to make them!) But their solution is pretty expensive (~$150) and I think I can do something for $80 from off the shelf parts.

Parts:

On the podcast, I said I thought it would cost around $80 to do a handcrafted one and my total here is $81. Yay estimating skills! Oh, I forgot:

  • Adorable receptacle: priceless.

On the show we also talked about features:

  1. Dolores would pat the bear.
  2. WiFi or other configuration to be set up by a daughter, Elizabeth, or other caregiver.
  3. Bear will also light when people hit a website (so the thinking of you functionality).
  4. System will message daughter (text, email, phone) if Dolores doesn’t pat the bear before certain times. (No check-in)
  5. System will also message daughter if bear is not charged or service is down for some reason.
  6. Daughter can see when the bear was patted (to verify system is working).
  7. Chargeable: 1 week of battery. Plugged in via USB wall wart.
  8. If battery is low (1/3 usable charge remaining or 3 days), system will email Dolores.
  9. If the battery is very low (2 or 1 day of battery left), system will email daughter.

Some I added later:

  • The bear will respond to a pat with an LED light (white if tricolor, green if red-green).
  • If the bear’s charge is low, it will respond to a pat with a blinking red light.
  • If you keep patting, the bear’s LED turns yellow. After thirty seconds of intense “patting”, the bear turns red and messages the family.

None of this is all that difficult. I looked at the Electric Imp hardware. I need to add battery monitoring so I’ll learn their Sampler class. And I need to add an I2C accelerometer but I already made an I2C LED work so I do not expect a problem. Much of this is server side, the agent software that I’ve been avoiding. I know in theory how to do it but I suspect the practice will be like hitting my head on a wall a few times.

Some of the work will be electronics: the battery is usable from 2.8V to 4.2V, the ADC input to the Imp presumably can’t go above it’s operating voltage (3.3V) so either I need to sort out a level shift (resistor divider? but that is a waste of power!) or figure out where else I can monitor power (maybe going into the Imp, on the April board?).

Also, I know an accelerometer is overkill, especially three axis ones; there are other motion sensors. On the other hand, I know how to use an accelerometer really well and I’ve got many lying around. Though I wonder… a temperature sensor would cost nothing but it takes longer to work and breaks my patting method.

Also, since the unit is going to be sleeping most of the time (for power reasons), the pat needs to wake it up. The Electric Imp has six pins available. I’m using 2 for I2C to the accelerometer, 1 for monitoring battery voltage, and 3 for the tricolor LED. If I need the accelerometer to interrupt the processor, I’m going to need to go to a bicolor LED so the user will only see red, green, and yellow. I think that is ok. But it makes me wonder if the accel really is the way to go. I don’t think I want a simple push button switch (not cute). Well, I need to go look at force sensitive resistors and soft potentiometers. Though how those are going to wake up the processor, I don’t know.

Yet.

h1

Cute internet enabled LED

August 29, 2013

Ok, I’m done. Or done enough.

Thinking of you LED

 

As you can see, I procured a jar and some semi-transparent rocks to diffuse the light. Recently the jar was mistaken for a jar of ice, so that’s what it looks like when it isn’t lit. The webpage looks like it did in the last post. It now posts the sender name as well as the color. I can snoop to see the server log with the sender name but didn’t make a page for the thinkee to see who sent them love when they weren’t looking.

The battery got bigger so that it lasts for about a week with 3-5 events/day. I did switch to a standard LED instead of my I2C LED so the total parts cost is about $70, pricey but it is handmade.

Things I kind of want to add to the project (projects are never finished):

  • Add battery monitoring so it does something when the battery needs charging
  • Add a thinkee page to see who sent love
  • Add an accelerometer so the thinkee can tap and have that get back to the sender
  • Update the Electric Imp to use the beta agent interface so the response is prettier after love is sent
  • I suppose some would say it needs a better container but I find this one quite amusing in its homemadeness.

I thought about selling this on Etsy but, really, I could sell this to a couple real-life people without bothering to learn how to do Etsy.

If I sell it, though, I don’t think I’d build another. I have a different idea. One that matters a more to me. But we’ll talk about that later. Soon.

 

h1

All that I need now is a jar

August 20, 2013

One thing that I didn’t really get before working on my thinking-of-you webpage: all web developers write OpenSource code. Once it is on the web, anyone can read it.

Copyrights are fine but is copying someone else’s method of selecting colors of setting up a form or changing font colors for a section instead of a div really protected? I’m not talking wholesale page stealing, just copying snippets.

I didn’t do anything that I didn’t attribute (or learn from HTML and CSS book I mentioned before). But I did read a few random pages, trying to see how they did this or that. That was kinda neat, looking at how pages are put together, now that I speak a little bit of the language. I understand why so many things are hidden in PHP layers: so no one can see a site’s undergarments.

Usually, my code is seen by one or two other people, during code reviews or when it is reused, not by anyone who cares to look. I don’t think I mind but it is a little odd. I mean, what if there was a better way to do it? On the other hand, I’m such a noob at web dev that I’m sure there is. And if you tell me, I’ll be happy to know. So why am I nervous?

Here it is:

Thinking Of You V1

https://logicalelegance.com/portfolio/thinking-of-you-wout-id.html

That won’t actually blink my LED. While it will let you set your name (and color if you don’t like the one it chooses), it will give you an error when you try to send some love to Christopher. (Hey, if I’m going to be working on this, might as well make it my sweetie.)

The response page is ugly, I’m going to ask Electric Imp about making the response prettier. I don’t care too much about the no-id get-error version but the one with the real key just sends back a page with OK in it sans prettifying.

Note that I met most of my goals: it is one page, settings are hidden after you put your name in, the cookie works (though Firebug can’t find it which is a pain for debugging). I also added my stretch feature of changing the button text on reload. That was easier than I expected and it amuses me.

I updated the Electric Imp code so with the right key in the html, I can change the LED. But I’ve decided to use a PWM LED instead of my fancy I2C LED. That shouldn’t take too long (Electric Imp has RGB LED example code). Then I’ll combine it my USB chargeable LiPo, then put it all in a jar with some hotglue.

 

h1

Nothing is ever wasted

August 19, 2013

I showed the thinking-of-you proof-of-concept webpage in the last post about this project. While functional, it was the definition of ugly. I want to make something lovely, that captures the charm of the project.

I have my sketch of what I want to do, it isn’t pretty (or legible to someone who isn’t me).

Web sketch

Here are the highlights:

  • I want the one file to be easily email-able.
  • One file to contain the three apparent pages: initial welcome, sending the (hug? wink? cheer?), and settings page to change name (and color).
  • The unused parts of the page shouldn’t be visible, the user should only see the welcome or normal page. The normal page should have a settings area that can be opened but is usually hidden.
  • The CSS and Javascript shouldn’t be in separate files. (One file, email, see first point.)
  • One file means no images. Off the shelf everything. But it should still look nice.
  • Colors are auto-assigned based on names but the user can still choose their color separately. This is a little complicated.
  • Color and name should be saved as a cookie so the user only does selection of these once.
  • Color and name should be changeable but that should be hidden until settings is selected on the normal page.
  • The color selected should be on the page itself: an icon should change color.

There is only one problem with all of that. It is way more web code than I know how to write. I started to solve this by reading Headfirst CSS and HTML. (I like the Headfirst series, they take longer to read but the information sticks a little better. The trade off is worth it, especially as they are amusing so I tend to keep reading.) I finished this book but I still didn’t know all I wanted to do, in particular the hidden parts of the page, the cookie, and showing the user’s color on the page.

Apparently, some of what I need to create is Javascript. I’m a little baffled as to the breakdown; HTML is the content and structure, CSS provides style, and Javascript does actions based on events. However, HTML5 can do actions based on events too. And none of them are particularly specific about how to deal with cookies, though Javascript seems the best for that so far. (Also Java has nothing to do with Javascript, they just named them that way to screw with my head.)

Yes, I program for a living but I don’t do this. And yes, I’ve made plenty of webpages, but they were either pure HTML (pre-2000) or done with a WYSIWYG editor. Plus, this is (secretly) an application masquerading as a webpage. Since all the cool kids are doing it, I thought it would be easy.

HTML5 (and all its friends) are living, changing standards. The books are out of date as soon as they are printed. Though, if you use a neat new feature, some browsers may not support it. (Goggle eyes in frustration of both never being right-according-to-the-spec and never knowing if I’m wrong-for-the-user’s-browser.)

This is strange to me… C99 (the C standard ratified in 1999) has been around long enough for me to know really well. And yet, Microsoft Visual Studio did not support it until last month. It isn’t that embedded systems move glacially slowly, the processors change quickly and new sensors are always coming out. I’m used to knowing and understanding the tricky parts of my language, it is bedrock to me. The shifting sands of web development are not giving me warm fuzzies.

On the other hand, I’m starting to get Agile development better now. This webpage will never be complete. It is getting better but there will always be something else I want to do. (For example, when you press the “Hug” button, it would be nice if the button changed color for about the same length of time that the widget’s LED will be on that color. That lets me have separate buttons for hug and wink since they’d have different times.) Even though the page isn’t perfect, I should put a line in the sand and say “this is pretty good”. That line would be easier to draw if I knew I could re-draw it in a week or two.

Thinking of you webpage v0

Here is the current version, as seen in Firefox. Some things to note:

  • This is the normal page, after the initial welcome page. My cookie is already set.
  • The turquoise heart is what my name colorizes to; other names would get a different color. I fix it so all colors have to be bright enough. (I’ll post the page next time so you can see your name’s assigned color.)

My list of short term improvements:

  • Hide settings area. I already mostly know how to do that (since I’ve already hidden the initial welcome area).
  • Fix cookie so it is not a session cookie but a permanent one. (Argh!)
  • Fix the color selector to look better in browsers that don’t support the color tag. (Chrome and Opera support color selection; Firefox and iPad do not, those will get a drop down list of colors. Right now you can type in a color but you have to guess what the HTML5 standard is thinking. Or write it in hex.)
  • Make hug and wink do separate things (this requires a little work on the Electric Imp widget as well). Or maybe remove one of them for now. Then I could change the name of the action every time you load the page. Hmm… move that to future features.
  • Do another pass of looking at the fonts and basic CSS to make it look more aesthetically pleasing.

My long term improvements are legion (and growing). They will probably change as I learn more about web development.

Wait a minute, why am I learning about web development? I’m a deeply embedded programmer.

I’ve been focused on getting this project done but I’m quite conscious of the fact that this information, this miniature education in web dev, would have been useful to me two years ago as I worked on an internet enabled fishbowl. (Note: it was not really a fishbowl but, to me, it was something that needed the internet as much as a fishbowl does.)

To connect the fishbowl to your home WiFi, you had to connect your computer (or phone) to the fishbowl’s WiFi network. The fishbowl would serve a page that would let you set your WiFi name (SSID) and password. Then the fishbowl would stop serving a webpage and go talk to the internet using the information you gave it. You’d switch your computer (or phone) and be able to talk to the fishbowl via the internet. Fun for all the fish.

But that webpage that the fishbowl would serve up? That came from the fishbowl’s miniature filesystem. And when we needed to make changes to it, the firmware team would go to the web dev team and ask for changes. They were always busy with the web (plus they spoke webese and we spoke embedded). Things took forever to get done. Well, that’s lame. Now, I could make those changes. I don’t think I’d change aesthetics but I’d be fine with 75% of the tweaks that needed doing.

So, if you think I’m learning web development for my thinking-of-you gadget, that is correct. But there is more to it than that. I hate the phrase “Internet of Things”, it oversells the connectivity that is coming. But whatever you call it, there will be more internet enabled fishbowls and now I can do even more for them.

Here fishy, fishy.