Category: Arduino

  • Current Sensing Transformers & EmonESP

    Current Sensing Transformers & EmonESP

    I’ve been upgrading my home power monitoring software a bit, as it looks like there are some new options out there for the ESP8266.

    Previously, I was using an ESP8266 with the Espressif AT firmware, available at their website: https://www.espressif.com/en/support/download/at

    Espressif also publishes an API reference for said firmware on their website as well. It looks like it’s even still being maintained – the AT Firmware API reference was last updated in August of 2020.

    You can see the code I used to interface with this AT firmware at my Github repository. Currently, that code runs on a Teensy 3.2 and pushes data using the AT firmware on the ESP8266 to an Emoncms server hosted on a Raspberry Pi. This method requires the generation of all sorts of AT commands and gibberish to get the ESP8266 to make a HTTP GET request to publish data to the Emoncms server. The main issue with this method was the fact that I was never able to get the ESP8266 to reliably reconnect to Wifi if the power went out. Essentially the ESP8266 would come up first, look for the Wifi network, get confused and just not play nice.

    I’ve been watching the development of EmonESP for a while – essentially the folks over at OpenEnergyMonitor wrote some great stuff that runs directly on the ESP8266 and provides a web UI that works great. What I’ve really been loving is the fact that there is a passthrough serial connection – so I can talk directly to my Teensy 3.2. This means I can remotely calibrate channels on the unit without re-flashing the unit, like I did before (great code writing by me… not).

    So after firing up Visual Studio Code with PlatformIO, I compiled the EmonESP for my device… and found a couple of issues. First, the path to the Emoncms server functionality was broken, so I made a fix and a pull request to get that fixed. Another issue was the fact that ArduinoJson (a great way to easily work with JSON on your Arduino or other microprocessor, FYI), declares sizing for objects up front – so the EmonESP implementation wasn’t cut out for my 20+ channels of data. Before, in src.ino in the EmonESP codebase:

      StaticJsonDocument<512> data;
    
      boolean gotInput = input_get(data);

    512 bytes got me about 16 channels of data + EmonESP diagnostic data. I’d like to keep the EmonESP diagnostic data it adds, plus some extra data for channels of temperature or other data. I sized it at 1024.

    This worked great! I’ve rewrote my code that runs on the Teensy 3.2 to hand off data in key:value pairs to the ESP8266 running the modified EmonESP software. That new sketch is located here.

  • Fusion 360 / Eagle PCB

    Fusion 360 / Eagle PCB

    It’s been a while since my last post! Every once and a while, I get a chance to sit down and work more on my power monitoring project. Previous versions of this project have been operational and collecting data for well on three years now, but I’ve wanted to clean up the physical design a bit and make it more usable.

    A quick recap of this project – my dad and I wanted something to monitor power usage in the home by each individual circuit in the house. Read about how these type of systems work at https://learn.openenergymonitor.org/.

    • I selected the Teensy 3.2 as my primary microprocessor to do ADC sampling and conversions as it has 21 ADCs
    • Selected the ESP8266 WiFi module to send gathered data to a database somewhere (using a local installation of EmonCMS on a Raspberry Pi)
    • Selected the Texas Instruments REF2033 as a voltage bias source (it’s expensive, but very nice for this application)
    • Used EAGLE to create schematics and the PCB layout
    • Sent PCB design files to SeeedStudio to be manufactured
    • Designed an enclosure in Fusion 360 with a snap-fit lid
    • Wrote (well, writing…) the code in the Arduino IDE

    A while back, North Carolina State University cut me off from using their Solidworks license – which makes sense – I graduated five years ago! In lieu of Solidworks, I picked up Fusion 360 at home (I use CATIA V5 at work).

    Since I last used EAGLE, it was purchased by Autodesk and woven into their portfolio. As such, EAGLE now supports some 3D CAD integrations with Fusion 360. I’ve completed a revised version of my power monitor PCB layout – shown below in both EAGLE and Fusion 360. The combo of these two programs makes modeling PCBs and their supporting enclosure/structure very easy.

    A page from the schematic
    The EAGLE PCB Layout

    Once you have the PCB design complete, you can go in and update the 3D model of each component in EAGLE. You upload and position a custom model in the EAGLE interface. The interface to do this is a bit confusing, but I hear it will be getting better and more straightforward in the future.

    Then, you click a button in EAGLE that pushes the model to Fusion 360. As you can see below, if you’ve routed all your traces, it even shows them on the board, as well as all vias and other tiny details. It’s actually pretty sweet.

    Looks pretty good, right? It’s all in the setup in EAGLE to get the push to Fusion to look great.

    This is what makes modeling enclosures super easy. In Fusion, you can actually project the board components onto sketches to drive geometry. Here’s a simple box I was able to come up with after couple of nights tinkering around:

    In reality, the box will be orange (that’s the only color of filament I have for the 3D printer at the moment)

    And for reference a section analysis:

    Section analyses are great for verifying you’ve got everything to fit!

    Sliced it up and printed it on my Qidi Tech I (A Flashforge/Makerbot clone):

    Orange.

    Now to update the software that runs on this bad boy…

  • PCB SolidWorks 3D modeling

    PCB SolidWorks 3D modeling

    This is actually work from a while ago – but I wanted to share this. I’ve been working on a new version of the Power Monitor PCB that uses an ESP8266 and Teensy 3.2. Here’s the board mocked up in SolidWorks CircuitWorks:

    And here’s the board in real life, with only a couple of components on it:

    Pretty neat, right?  You can spend as much time as you want in SolidWorks CircuitWorks to get everything as photo-realistic as possible. The important thing is to get component heights, sizes, and locations right to make the design of an enclosure easier.

  • Motorcycle Computer Revisit

    About a year ago, I made this motorcycle computer that mounts to the dash of my 2012 V-Strom 650. It controlled my heated jacket and my heated grips.

    I have access to a 3D printer at work, so I modeled some housings in Solidworks and printed them out. It’s awesome – 3D printers make prototyping and hobby projects really easy! The housing is held in place to the anodized aluminum plate with standard automotive RTV.

    Front
    Rear

    The below box housed the ‘brains’ and resided under the seat. This left the signal lines to the screen and rotary encoders exposed to a LOT of interference. My new plan is to move the Teensy 3.2 into the same housing as the screen, and make a much smaller box to house only the MOSFETs, 12v-5v DC-DC converter, and a logic-level converter to ensure the MOSFETs see a signal that is above their minimum spec to ensure the gate is pulled high.

    Spaghetti.

    This setup has been on the motorcycle for about a year – and no real ill effects were noticed. I never once had to fix anything. The code could use a bit of work to make the interface a more intuitive, but that’s also part of this revisit.

    I’m also planning on adding some new sensors to the setup. I’ve got a 10DOF board from Adafruit laying around, which will allow me to use a magnetometer, accelerometer, gyroscope, and barometer. It communicates via I2C and each device on the board has a unique ID. I actually wrote drivers for this board myself in Python a while ago – the should be available on my Github. I have a GPS module that I’d also like to give a try – it will allow me to get velocity and position data. It communicates over a UART connection.

    GPS, 6DOF Sensor, Teensy 3.2

    I set about making a new housing for this configuration in Solidworks. I’m planning on only using one CAT5 cable (8 conductors) to bring power, MOSFET PWM signals, and a 3.3v reference voltage back to the rear module.

    I do have a few problems/questions I need to work out – for instance – will the GPS be able to get a reliable lock from this position, or do I need an external antenna? How about easy firmware updates? Do I need to keep both encoders and buttons?

    I began to take apart the old setup. Here’s a quick picture after I took off the 3D printed cover and one of the CAT5 connectors.

    More Spaghetti.
  • Json-enabled WiFi screen

    Json-enabled WiFi screen

    Along with the power monitoring project I’ve been working on, I’ve also developed a simple Internet-enabled screen. It uses another ESP8266, a few wires, and a 16×4 LCD display.

    Not the worst looking thing, but not exactly cute, either.
    Seems like a good candidate for 3D printing.

    The display used is one of the standard HD44780 compatible ones – so you can use the LiquidCrystal library, which is one of the standard libraries in the Arduino IDE.  Wiring diagram to a standard Arduino is available here, as well as the library reference and documentation. You can find these types of displays in various sizes, ranging from $5 to $20. You’ll also need a 10k potentiometer to control the display’s contrast.

    The ESP8266 variant used is the WeMos D1 Mini – http://www.wemos.cc/Products/d1_mini.html.  It’s available online for about $4.

    For the box, I used a couple of scrap pieces of 1/4 inch oak plywood, and a scrap piece of 1×8 pine board. I measured the screen, cut, and gave the USB wire a small hole. Here it is painted and all put together:

    To help parse JSON, I used this fantastic library, called Arduino JSON (clever name). It’s pretty easy to use and is well documented.

    To program an ESP8266 using the Arduino IDE, you can also use a custom board type available here.

    My simple code is available on my Github page. It interprets the JSON, formats it to fit on the screen, and scrolls through it.  Someday I might add a button or rotary encoder to help control it.

  • Power Monitoring Code

    There are two chips that make up this board – the Teensy 3.2 and the ESP8266. The ESP8266 in this setup runs the AT firmware, which allows communication over serial using a published API. The API documentation is available here. To flash the ESP8266 with the AT firmware, Expressif (the manufacturer) has a tool available on their website.

    The default baud rate of the most recent AT firmware is 115200. This can be changed, but the faster the better, right?

    To use the Arduino IDE to program the Teensy 3.2, you can download Teensyduino, available from here.

    So now our environment for programming this combo is ready!

    To test the connection between the ESP8266 and the Teensy 3.2, you can throw on a simple passthrough sketch, and manually type in AT commands into the serial monitor. (also for some reason I can’t figure out the WordPress code formatting)

     

    void loop() {
    // Send bytes from ESP8266 -> Teensy -> Computer
    if ( Serial1.available() ) {
    Serial.write( Serial1.read() );
    }
    // Send bytes from Computer -> Teensy -> ESP8266
    if ( Serial.available() ) {
    Serial1.write( Serial.read() );
    }
    }

    Make sure you put initialize both serial ports to 115200 in setup().

    The complete code is available at https://github.com/fillycheezstake/PowerMonitor. It’s a WIP, so don’t judge too hard.

  • Power Monitoring Update

    Power Monitoring Update

    So, after a… while… I finally got around to getting this project together.

    Below, a quick picture of the completed and populated board. This required quite a bit of soldering, and I discovered a couple of problems during testing (hey, it’s supposed to be  a learning experience!).

    This was fun to solder!

    The Teensy 3.2 and the ESP8266 are removable – they have plugs for all their pins.

    So the finished board has 12 inputs. The board size is the limit of the free version of EAGLE. On the back is where you’ll see some airwires (aka mistakes).

    Oops.

    Two airwires – one because I forgot to route a pin from the Teensy 3.2 to the ESP8266, and the other because I may have fried one of the input pins on the Teensy trying to get the device powered from AC.

    So when I was designing the board in EAGLE, I knew a lot less about circuit design than I do now (especially AC power). During testing, I discovered that powering the circuit via AC isn’t really that feasible for high currents (the ESP needs >200ma). Not only does the taking power from the AC-AC transformer deform the AC waveform slightly (affecting measurement), you also must use a separate ground for the ADCs.  Oops. Here’s some blog posts describing this –  here and here.

    I haven’t yet uploaded the code to my GitHub account – that’s coming in a future update on the code that supports this project.

  • Power Monitor PCBs arrived today!

    Power Monitor PCBs arrived today!

    I got something in the mail today from Seeedstudio!

    Bussin’

     

    And here they are:

    Packaged well.

    Here’s some close up shots of both sides:

    I’m very impressed with the quality and value. I only paid $26 for five of these boards! I’ll definitely be ordering from Seeedstudio again in the future. So now the rest of the components have to arrive, and then I’ll be able to begin soldering components to the board.

    The last post describes a bit about the design process.

  • Circuit Design / Personal Energy Monitoring

    Circuit Design / Personal Energy Monitoring

    A while ago, I stumbled upon this great organization: OpenEnergyMonitor. They’re doing a lot of things right!

    Anyway, they prompted me to attempt to create a derivative of their EmonShield TX. I loved the idea, but I wasn’t sold on the RF nature that needed a base station. Since it’s easy to provide WiFi connectivity to an Arduino with the venerable ESP8266, why not make it so? Also the number of circuits (four) wasn’t exactly calling my name. I wanted the ability to do more.

    So.

    For non-intrusive current monitoring, current clamps are great. They’re simple current transformers – they themselves are coils of wire, and the conductor around which the clamp is placed forms the other coil.  Thus two things are achieved – isolation and voltage step-down. However, without a burden load, transformers can be dangerous – they build up potential until they reach their insulation’s break-down voltage and fail (sometimes dangerously). So to use these, you MUST have in place a burden resistor, before clamping around a current carrying wire. OpenEnergyMonitor provides a great circuit diagram describing how to both limit the peak voltage, and bias it to remove negative potential (for measurement reasons).  The burden resistor must also be matched so the peak voltage produced by the CT doesn’t saturate or exceed the limits of the device used to measure it.

    Once the CT output is biased and limited to the correct voltage by the burden resistor, it’s ready for measurement by an Analog-to-digital converter.

    To measure voltage, you can simply use a 9VAC transformer to provide a voltage signal (you can also use it to power the contraption). It just takes a bit of biasing and dividing, similar to the CT sensors. Again, OpenEnergyMonitor has diagrams here.  If you wanted to measure both phases of common residential service, you’d need two, and thus two ADC inputs on the measurement device.

    In the US, to measure a 120v device, you need 1 CT sensor and 1 ADC input. However, to measure a 240v device, you must measure both phases and add them together. So each 240v device requires two ADC inputs.

    Now that we have low voltage analog signals, how to we read them? Arduino time! or… not.

    An Arduino Uno has 6 ADC inputs. Not enough for me.

    An Arduino Mega has 16 ADC inputs, but is rather large, and there are many reports of being unable to power the hungry ESP8266. Again, out.

    The Teensy 3.2 board has many more features and ADC inputs in a much smaller package. I use the same board in my revamped Motorcycle Computer project. Its 3.3v regulator can also support the ESP8266’s power requirements. Bingo.

    Now, for the board. I’ve been using EAGLE 7.5 to learn and play around with circuit design. Since this circuit doesn’t have any high speed signals (only 60hz AC), I don’t need to worry much path impedance or other factors in design.  Here’s what I was able to come up with:

    The board layout. I experimented with some surface mount components in the bottom left, but the rest of the components are all through hole.

    And now that I’ve got it designed, I sent it away for manufacturing. I used SeeedStudio for my first ever self-designed PCB creation.  The next update will likely be when I get these in the mail. I’ll make sure to note the turnaround time as well.

  • Motorcycle Computer: The Screen Module

    Motorcycle Computer: The Screen Module

    I recently found some pictures of construction that I thought I lost, so I’ll be able to do a simple breakdown of the components.

    First – this shelf from AdventureTech, LLC.  Here’s the link to the product page. They’ll even cut holes in it for you, although I cut my own.

    From AdventureTech, LLC
    Image is from AdventureTech, LLC

    Once I had the shelf – it’s just a nice powder coated piece of aluminum – I cut out a rectangle for the LCD, and drilled holes for the buttons and rotary encoders I planned to use. Then I soldered wires to all the terminals I’d need – and then routed them to Cat5e jacks, so the actual computing power would be somewhere else on the bike.

    Part of this meant I’d need something to keep the electronics safe and hopefully dry. I modeled a cover in SolidWorks and used a Makerbot Replicator 2 to 3D print a cover. The SolidWorks file is here. I used Black Permatex RTV to seal any openings.

    As stated in another post (this one) I needed to remove I2C from the equation, so that I2C backpack was later removed.

    Here’s a quick parts list:

    • 2x Rotary Encoder – Link
    • 2x LED Button – Link
    • 1x LCD – Link
    • 3x Cat5e Jacks
    • Suzuki V-Strom Shelf (AdventureTech, LLC) – Link
    • 3D Printed Cover
    • Black RTV