The Smell of Molten Projects in the Morning

Ed Nisley's Blog: Shop notes, electronics, firmware, machinery, 3D printing, laser cuttery, and curiosities. Contents: 100% human thinking, 0% AI slop.

Category: Electronics Workbench

Electrical & Electronic gadgets

  • There’s No Undo Key in CNC

    The Axis user interface for EMC2 has a manual command entry mode, wherein you can type G-Code statements and EMC2 will do exactly what you say. That’s handy for positioning to exact coordinates, but I rarely use it for actual machining, as it’s just too easy to mis-type a command and plow a trench through the clamps.

    OK, on a Sherline mini-mill, you’d maybe just snap off a carbide end mill, but you get the general idea.

    I was making a simple front panel from some ancient nubbly coated aluminum sheet. The LCD and power switch rectangles went swimmingly.

    Then I tried to mill an oval for the test prod wires using G42.1 cutter diameter compensation. I did a trial run 1 mm above the surface, figured out how to make it do what I wanted, then punched the cutter through the sheet at the center of the oval and entered (what I thought were) the same commands by picking them from the history list.

    EMC2 now handles concave corners by automagically inserting fillets, so it must run one command behind your typing. I drove the cutter to the upper-right end of the oval (no motion) so it could engage cutter comp mode, entered the G2 right endcap arc to the lower edge (cuts straight to upper right), and then did something wrong with the next command.

    Epoxy-patched front panel hole
    Epoxy-patched front panel hole

    The cutter carved the endcap properly, then neatly pirouetted around the end and started chewing out an arc in the other direction. Even looking at the command trace I can’t figure out what I mistyped, but as it turns out it doesn’t matter… I was using the wrong dimensions for the hole anyway.

    So it’s now patched with epoxy backed up by a small square of aluminum. When it’s done curing, I’ll manually drill a pair of holes at the right coordinates, manually file out the oval, shoot a couple of coats of paint, and it’ll be OK.

    Nobody will ever know!

    If I recall correctly, Joe Martin of Sherline was the first person to observe that, unlike word processing programs, CNC machines lack an Undo key…

    Update: Like this…

    Patched panel - rear view
    Patched panel – rear view

    The shoot-a-couple-of-coats thing did not go well: a maple seed landed on the front panel. Ah, well, it’s close enough. Here’s a trial fit; the bellyband height extenders on the sides need a dab of epoxy and a shot of paint, too, but I may never get a round ‘tuit for that.

    Front panel trial fit
    Front panel trial fit

    It’s the long-awaited Equivalent Series Resistance meter…

  • Arduino Library for (Old?) Optrex DMC-family LCDs

    Having determined that the existing Arduino LiquidCrystal library routine wouldn’t work for the Optrex DMC-16117 character LCD in my parts heap, I decided to modify it to meet the data and timing requirements mentioned in the datasheet. This is sufficiently slow and old that it should work for contemporary displays built around the Hitachi HD44780 and its ilk, but I certainly haven’t tested it!

    The straight dope on building an Arduino library from scratch is there, but there’s no need to work from First Principles here.

    Start by copying the library files (this FLOSS stuff is wonderful that way), renaming them, and changing all the LiquidCrystal strings to LCD_Optrex:

    cd /opt/arduino/hardware/libraries/
    cp -a LiquidCrystal LCD_Optrex
    cd LCD_Optrex
    rm LiquidCrystal.o
    rename 's/LiquidCrystal/LCD_Optrex/' LiquidCrystal.*
    for f in LCD* k*; do sed -i 's/LiquidCrystal/LCD_Optrex/g' $f; done
    cd examples
    for f in * ; do sed -i 's/LiquidCrystal/LCD_Optrex/g' $f/$f.pde
    cd -
    

    You could do that by hand with an editor if you prefer.

    Depending on how you’ve installed the Arduino files, you may need a sudo to make that work. Better, perhaps, to tweak the permissions for (at least) the LCD_Optrex directory & files therein to grant yourself write access.

    I created a sendraw4() function to send a single 4-bit nibble during the startup sequence, so add that to the private section of LCD_Optrex.h:

    private:
    void send(uint8_t, uint8_t);
    void sendraw4(uint8_t);
    

    The new code is in LCD_Optrex is shamelessly adapted from the existing send() function, minus the mode selection and 8-bit stuff:

    void LCD_Optrex::sendraw4(uint8_t value) {
      digitalWrite(_rs_pin, LOW);
      digitalWrite(_rw_pin, LOW);
    
      for (int i = 0; i < 4; i++) {
        digitalWrite(_data_pins[i], (value >> (i + 4)) & 0x01);
      }
    
      digitalWrite(_enable_pin, HIGH);
      digitalWrite(_enable_pin, LOW);
    }
    

    If I were doing this from scratch, I’d use d7 through d4 rather than d3 through d0 to match the datasheet, but that’s a stylin’ thing.

    Replace the existing LCD panel setup code with an exact mapping of the datasheet’s procedure. For the 4-bit setup, it goes a little something like this:

    delayMicroseconds(16000);       // mandatory delay for Vcc stabilization
    sendraw4(0x30);                 // set 8-bit mode (yes, it's true!)
    delayMicroseconds(5000);        // mandatory delay
    sendraw4(0x30);
    delayMicroseconds(200);
    sendraw4(0x30);
    delayMicroseconds(40);          // command delay
    sendraw4(0x20);                 // finally set 4-bit mode
    delayMicroseconds(40);          // command delay
    
    command(0x28);            // 4-bit, 2-line, 5x7 char set
    command(0x08);            // display off
    command(0x01);            // clear display
    delayMicroseconds(16000);
    command(0x06);            // increment, no shift
    command(0x0c);            // display on, no cursor, no blink
    

    It seems you cannot use the delay() function in the constructor, as interrupts and suchlike aren’t active. The delayMicroseconds() function disables & enables interrupts; I don’t know if that is a Bad Thing or not.

    The 8-bit initialization code, which I haven’t tested, doesn’t need the sendraw4() function, but does need the same alterations. Apart from enabling 4-bit mode, of course.

    Various commands have different timing requirements, as shown on page 39 of the DMC16117 datasheet. Add a delayMicroseconds(16000); to the clear() and home() functions, then add delayMicroseconds(40); to the send() function, like this:

    void LCD_Optrex::clear()
    {
      command(0x01);  // clear display, set cursor position to zero
      delayMicroseconds(16000);
    }
    
    Optrex DMC16117 Instruction Timing
    Optrex DMC16117 Instruction Timing

    With all that in place, fire up the Arduino IDE and compile one of the example programs. That will build the LCD_Optrex.o file, too. If you have such a display, either wire it up as indicated or change the example code to match your connections.

    What should happen is that the LCD should initialize correctly under all conditions… how’s that for anticlimactic?

    Here’s an OpenOffice document with LCD_Optrex.h, LCD_Optrex.cpp, and examples.txt all in one lump: LCD_Optrex Library Files.odt. Save each section as a separate flat-ASCII text file with the appropriate name in the right spot and you’re in business. I’d present a ZIP file, but WordPress isn’t up to that.

    Memo to Self: A function to write a 16-character string to the stupid 16-character DMC16117 which has a single row that’s addressed as two 8-character lines would be nice. That requires keeping track of the current cursor position, which could be tricky. Maybe I should just scrap those suckers out?

  • Arduino LiquidCrystal Library vs Old HD44780 LCD Controller

    I recently attached an ancient Optrex DM16117 LCD to an Arduino and discovered that the standard LiquidCrystal library routine wouldn’t initialize it properly. After turning on the power, the display would be blank. Hitting the Reset button did the trick, but that’s obviously not the right outcome.

    It turns out that initializing one of these widgets is trivially easy after you realize that the data sheet is required reading. If you do everything exactly right, then it works; get one step wrong, then the display might work most of the time, sorta-kinda, but most likely it won’t work, period.

    The catch is that there’s no such thing as a generic datasheet: what you must do depends on which version of the HD44780 controller lives on the specific LCD board in your hands and what oscillator frequency it’s using. The LiquidCrystal library seems to be written for a much newer and much faster version of the HD44780 than the one on my board, but, even so, the code may not be following all the rules.

    Optrex DMC16117 Initialization Sequence
    Optrex DMC16117 Initialization Sequence

    To begin…

    Fetch the Optrex DMC16117 datasheet, which includes the HD44780 timings for that family of LCD modules. There’s also a datasheet for just the Optrex LCD module itself, which isn’t quite what you want. You could get a bare Hitachi HD44780 datasheet, too, but it won’t have the timings you need.

    Pages 32 and 33 of the DMC16117 datasheet present the 8-bit and 4-bit initialization sequences. Given that no sane engineer uses the 8-bit interface, here’s the details of the 4-bit lashup.

    Two key points:

    • The first four transfers are not standard command sequences
    • The delays between transfers are not negotiable

    The starting assumption is that the LCD has not gone through the usual power-up initialization, perhaps because the supply voltage hasn’t risen at the proper rate. You could drive the LCD power directly from a microcontroller pin for a nice clean edge, but most designs really don’t have any pins to spare for that sort of nonsense: code is always cheaper than hardware (if you ignore non-recurring costs, that is, as many beancounters do).

    The Arduino LiquidCrystal library routine initialization sequence (in /opt/arduino/hardware/libraries/LiquidCrystal/LiquidCrystal.cpp) looks like this:

    command(0x28);  // function set: 4 bits, 1 line, 5x8 dots
    command(0x0C);  // display control: turn display on, cursor off, no blinking
    command(0x06);  // entry mode set: increment automatically, display shift, right shift
    clear();
    

    The four-bit version of the command() function sends both nibbles of its parameter, high followed by low, which simply isn’t correct for the first few values the DMC16117 expects. Worse, the timing doesn’t follow the guidelines; there’s no delay at all between any of the outgoing values. Again, this is most likely due to the fact that LiquidCrystal was written for a newer version of the HD44780 chip.

    After a bit of fiddling around, I decided that the only solution was to create a new library routine based on LiquidCrystal with the proper delays and commands: LCD_Optrex. It might not work for newer LCDs, but at least it’ll play with what I have in my parts heap.

    Next, the gory details…

    Memo to Self: The protracted delay after the first Clear is absolutely vital!

  • Recommended Pliers

    Having just finished tweaking the nosepieces on my new sunglasses into shape, it’s worth mentioning a few pliers you should have.

    This set of pliers (PN HH02075SET) from Circuit Specialists is absolutely invaluable. Mine were about twice the current price; the picture looks the same.

    You’ll use these four metal-forming pliers (PN 60398) from Micro-Mark somewhat less often, but when you need ’em (like for adjusting your glasses), you need ’em bad. Mine were about half the current price, but I’m sure they cost the same in constant dollars.

    I picked up a bunch of surplus Plato 170 flush cutters a long time ago, but even their current price isn’t too forbidding. Great for circuit board work.

    Invest in tools: put your money in metal!

  • Arduino Push-Pull PWM

    Push-Pull Drive: OC1A top, OC1B bottom
    Push-Pull PWM Drive: OC1A top, OC1B bottom

    Most of the time you need just a single PWM output, but when you’re driving a transformer (or some such) and need twice the primary voltage, you can use a pair of PWM outputs in push-pull mode to get twice the output voltage.

    A single PWM output varies between 0 and +5 V (well, Vcc: adjust as needed), so the peak-to-peak value is 5 V. Drive a transformer from two out-of-phase PWM outputs, such that one is high while the other is low, and the transformer sees a voltage of 5 V one way and 5 V the other, for a net 10 V peak-to-peak excursion.

    A 50% duty cycle will keep DC out of the primary winding, but a blocking capacitor is always a good idea with software-controlled hardware. A primary winding with one PWM output stuck high and the other stuck low is a short circuit that won’t do your output drivers or power supply any good at all.

    An Arduino (ATMega168 and relatives) can do this without any additional circuitry if you meddle with the default PWM firmware setup. You must use a related pair of PWM outputs that share an internal timer; I used PWM 9 and 10.

    #define PIN_PRI_A   9    // OCR1A - high-active primary drive
    #define PIN_PRI_B   10   // OCR1B - low-active primary drive
    

    I set push-pull mode as a compile-time option, but you can change it on the fly.

    #define PUSH_PULL   true // false = OCR1A only, true = OCR1A + OCR1B
    

    The timer tick rate depends on what you’re trying accomplish. I needed something between 10 & 50 kHz, so I set the prescaler to 1 to get decent resolution: 62.5 ns.

    // Times in microseconds, converted to timer ticks
    //  ticks depend on 16 MHz clock, so ticks = 62.5 ns
    //  prescaler can divide that, but we're running fast enough to not need it
    
    #define TIMER1_PRESCALE   1     // clock prescaler value
    #define TCCR1B_CS20       0x01  // CS2:0 bits = prescaler selection
    

    Phase-Frequency Correct mode (WGM1 = 8) runs at half-speed (it counts both up and down in each cycle), so the number of ticks is half what you’d expect. You could use Fast PWM mode or anything else, as long as you get the counts right; see page 133 of the Fine Manual.

    // Phase-freq Correct timer mode -> runs at half the usual rate
    
    #define PERIOD_US   60
    #define PERIOD_TICK (microsecondsToClockCycles(PERIOD_US / 2) / TIMER1_PRESCALE)
    

    The phase of the PWM outputs comes from the Compare Output Mode register settings. Normally the output pin goes high when the PWM count resets to zero and goes low when it passes the duty cycle setting, but you can flip that around. The key bits are COM1A and COM1B in TCCR1A, as documented on page 131.

    As always, it’s easiest to let the Arduino firmware do its usual setup, then mercilessly bash the timer configuration registers…

    // Configure Timer 1 for Freq-Phase Correct PWM
    //   Timer 1 + output on OC1A, chip pin 15, Arduino PWM9
    //   Timer 1 - output on OC1B, chip pin 16, Arduino PWM10
    
     analogWrite(PIN_PRI_A,128);    // let Arduino setup do its thing
     analogWrite(PIN_PRI_B,128);
    
     TCCR1B = 0x00;                 // stop Timer1 clock for register updates
    
    // Clear OC1A on match, P-F Corr PWM Mode: lower WGM1x = 00
     TCCR1A = 0x80 | 0x00;
    
    // If push-pull drive, set OC1B on match
    #if PUSH_PULL
     TCCR1A |= 0x30;
    #endif
    
     ICR1 = PERIOD_TICKS;           // PWM period
     OCR1A = PERIOD_TICKS / 2;      // ON duration = drive pulse width = 50% duty cycle
     OCR1B = PERIOD_TICKS / 2;      //  ditto - use separate load due to temp buffer reg
     TCNT1 = OCR1A - 1;             // force immediate OCR1x compare on next tick
    
    // upper WGM1x = 10, Clock Sel = prescaler, start Timer 1 running
     TCCR1B = 0x10 | TCCR1B_CS20;
    

    And now you’ll see PWM9 and PWM10 running in opposition!

    Memo to Self: remember that “true” does not equal “TRUE” in the Arduino realm.

  • Arduino Connector & Hole Coordinates

    Arduino Diecimila
    Arduino Diecimila

    If you’re building an Arduino shield, you must align the connectors & holes with the Arduino board underneath. That seems to be easy enough, assuming you start with the Eagle CAD layout found there, but when you’re starting with your own layout, then things get messy.

    Here’s how to verify that you have everything in the right spot, at least for Diecimilla-class boards. Start by holding the Arduino board with the component side facing you, USB connector on the upper left. Rotate your own PCB layout appropriately or stand on your head / shoulders as needed.

    With the exception of J3, the center points of the connectors & holes seem to be on a hard 25-mil grid with the origin at the lower-left corner of the board (below the coaxial power jack):

    • J3 (AREF) @ (1.290,2.000)
    • J1 (RX) @ (2.150,2.000)
    • POWER @ (1.550,0.100)
    • J2 (AIN) @ (2.250,0.100)
    • Upper-left hole = 0.125 dia @ (0.600,2.000)
    • Upper-right hole = 0.087 dia @ (2.600,1.400)
    • Lower-right hole = 0.125 dia @ (2.600,0.300)
    • Reset button = (2.175,1.065)

    Offsets between points of interest:

    • connector rows Y = 1.900
    • right holes Y = 1.100
    • UL hole to UR hole = (2.000,-0.600)
    • UL hole to J3 X = 0.690
    • J3 to J1 X = 0.860
    • J3 to POWER X = 0.260
    • POWER to J2 X = 0.700
    • J1 to UR hole = (0.450,-0.600)
    • J2 to LR hole = (0.350,200)

    Note that the three etched fiducial targets are not on the 25-mil grid. They’re not on a hard metric grid, either, so I don’t know quite what’s going on. Fortunately, they’re not holes, so it doesn’t matter.

    Memo to self: perhaps I’ve measured & calculated & transcribed those values correctly. Double-check before drilling, perhaps by superimposing double-size PCB layouts on a light table or window. Finding it then is much less annoying than after drilling the board… ask me how I know.

  • Homebrew V-750 Dosimeter Charger Pedestal: LED Holder, Spring, and Assembly

    Drilling mounting holes in base ring
    Drilling mounting holes in base ring

    The charger pedestal includes an LED to light up the dosimeter’s graticule. I seated a 10 mm white LED into a polycarbonate ring that also serves as the base for the stiff spring that presses the contact assembly against the dosimeter’s internal spring.

    I made the base while I was doing the lathe work for the contact assembly, then grabbed it in the Sherline mill’s 3-jaw chuck to drill the 4-40 holes with a touch of manual CNC.

    As before, I manually tapped the holes, but it’s a lot easier with each hole at the right location and pointed in the right direction!

    Step bit making an annular ring
    Step bit making an annular ring

    I described the step-drilling that produced the correct hole and shoulder sizes there. That won’t work every time, but in this situation it was just about perfect.

    The LED power wires pass through the central hole in the ring. I used a blob of hot-melt glue to hold the LED in place; epoxy would be more in keeping with the nuclear weapons theme, but HMG is just fine with me.

    There’s another hole just to one side of the LED, more or less centered between the mounting screws, that passes the wire from the dosimeter charging contact out of the pedestal. This wire starts at the center of the top, passes inside the spring, and must not be pinched along the way.

    LED in base with spring positioning ring
    LED in base with spring positioning ring

    I added an aluminum cylinder as a positive stop to prevent the dosimeter contact assembly from getting pushed too far into the pedestal. The length matches up with the anti-rotation slot in the EMT: the screw doesn’t quite hit the top or the bottom of the slot.

    A wrap of green electrical tape around the outside made the cylinder a slip fit inside the EMT shell. It shouldn’t move at all.

    The cylinder also holds the spring in place so it can’t rub against the charging wire, but I’m pretty sure that isn’t necessary.

    The spring comes from my parts heap. It must provide a bit under 8 pounds of force to activate the dosimeter charging spring with about 3 mm of travel. I picked the length of the EMT shell to preload the spring to make the answer come out right, which also affects the length of the aluminum cylinder.

    The spring OD must fit into the EMT and the ID must clear the 10 mm LED and charging wire in the base. Your mileage will most certainly vary.

    Charging pedestal components
    Charging pedestal components

    Assembly is straightforward, but goes much more easily with three hands.

    • Screw the panel mount bolt into place
    • Attach the charging wire to the central contact & remove the anti-rotation screw
    • Slide the central contact in place, reinstall the screw through the slot
    • Slide the spring & aluminum cylinder in place, wire in the middle
    • Pass the wire through the LED base ring
    • Press the base assembly into position and hold while installing the screws
    Finished charging pedestal
    Finished charging pedestal

    The charger I built turns the LED and charger power on with a push-to-activate digital encoder knob, so there’s no need for the 1 lb spring & switch found in the V-750 charger.

    To read the dosimeter, just hold it loosely atop the pedestal, push the twiddle knob down, and the LED comes on.

    To zero the dosimeter, press it firmly and twiddle the knob for zero!

    I’ll describe the charger circuity at some point; it’s detailed in my Circuit Cellar column in the August 2009 issue.

    V-742 Dosimeter set to Zero
    V-742 Dosimeter set to Zero