Sunday Morning Experimenting

I figured after a couple of horrendous days figuring out what was wrong with my ROMS, it was time for a morning’s relaxing.  Some time ago I added I2c to the Home Control software but never actually got around to doing anything with it.  The i2c parallel expansion modules I got from Ebay have their own pull-up resistors on them… could I get away with hooking 2 boards up without having to dismantle surface-mount pull-ups on on?

test ledsSo first things first I wired up some test LEDs comprising a some AQUA LEDs I had lying around and 120r resistors. I looked all over for pre-made leads with female 0.1” connectors on them but as usual ended up making up my own. If anyone knows of a good cheap source of these – do let us know in here.

Next stop I needed a test board and the obvious one to use was one of the samples I got from Bob Elmour.

A few extra ground, 3v3 and 5v lines (0.1” connector strip) along with GPIO4 and GPIO5 brought out and I was in business. Ok, not the prettiest layout but good enough for testing.

And… yes, I’m not sure I’d want to go beyond two without removing resistors – but they work. With one DIP switch set to ON-ON-ON and the other to ON-ON-OFF I found I could easily use i2c to turn on and off LEDS on both boards at addresses 39 and 38 respectively.

ESP8266 board

It is worth noting that the output DRIVE capability of the little red boards is not that good (as the outputs are also used as inputs with pull-ups) and so to get a decent output I fastened the LED + leads to +3v3 and used the expander boards to SINK current (which means the output logic is inverted).

Using the come control software, lighting up those 4 LEDS is simple.

{i2c:39,0,3}

{i2c:38,0,3}

(The zero is a recent addition and simply says there is nothing being returned. See future blogs for more extensive use involving multiple parameters and an optional return value).

So for the loss of 2 wires, you get 16 outputs using I2c and at a cost for boards of around a fiver. Not entirely sure how well that compares to sticking an ATMEGA24560 onto the ESP and doing some custom software to let you control the outputs (I have thought about this)… but it is certainly one easy way to get more inputs and outputs.

21 thoughts on “Sunday Morning Experimenting

  1. Right – I’ve ordered 2 of those ARM boards – amazing – £1.50 each – couple of weeks – looking forward to putting i2c on them and seeing what they can do.

  2. Peter,

    This sounds good as does the follow up blog – really interested in being able to use the Mega etc for multiple DS18B20 Temp Measurement and conversion and then return the result for the ESP to send out via MQTT

    What needs to be done (happy to help/provide testing) to get the Arduino code parsing these commands and then providing more than a single byte of returned data (Temp to maybe 1 decimal place)

    Craig

    1. So the simple answer Craig – is that I need to learn how to make better use of the I2c. Part of the issue is that the ESP version is a low-level thing with handshakes etc – and the Arduino version is higher level, more abstract – which does not help when you are picking this up. I would have thought that when the slave sends data back there would be a way to see how many characters are coming in if more than one – but on the ESP side I’m not seeing it yet. I’ll have a play later today – because yes, arbitrary data either way would be good. At first I was going to use serial as I have line parsers available at both ends – but I think yes, i2c is a good idea – just need a slightly better grip on it – I also want the Arduino to hold back on the response until it has performed the tasks and as this is done in callbacks I’m not yet sure the best way to perform this – I’ll maybe put the Arduino code in here and let people feed back – anyone think they’re a whizz at Arduino slave I2c?

      1. It looks like the only way to do this with the i2c master/slave mode is have the ESP poll the slave device. The initial command call from the master could include a simple sequence number, so that the master and slave both know which instance of the command they’re talking about during when polling.

        1. I do need to do this in one operation (or two connected operations) – the very asynchronous nature of Node-Red would make things awkward otherwise. I must be missing some kind of handshaking somewhere.

    1. Good – but more expensive than the microprocessor it is serving – a MEGA costs less than a fiver… with Arduino I would imagine the pullups are so high you could fasten a few onto the bus.

      Thanks for sharing.

  3. I’ll look forward to that Peter. BTW I should have added the library info was for those people who use the arduino/esp IDE. I know you are ‘bare metal’ C in Eclipse man!

    1. Well Bob – people change – all the Arduino code (I say Arduino – I mean Arduino-like – I’m using a more powerful 1284 but the code is the same up to now) is written using the Arduino 1.6.9 IDE along with the latest libraries – and I’ve detailed in the blog you’ve not read yet but no doubt are about to – where to get the 1284 board plug in.

      Indeed not only that but I’m half way through a conversion of my line parser to make a simplified version of my software in the Arduino environment so I can add in some of the sensor libraries. I don’t like that serial terminal – it has no line memory!!!

      So – read the new blog – which describes changes to the ESP code as of today regarding i2c – and the implementation of an Arduino type device as a slave – all working – just got to get my head around now much overhead I can put into the receiveEvent() as I have to do my coding in there, to get valid data back in the requestEvent() function. It’s all starting to clear in the head – but as it is now 11.30pm here in Spain I think I may stop at this point and point some social media to where it is right now..

      Enjoy.

      1. Very interesting Peter, I do like the idea of utilizing the massive IO of a Mega or similar. I have only Uno’s, Mini’s and Mega so that tends to be what I work with.
        I’ll look forward to following your blog on this subject.
        Likewise at 11:55 in the UK I need to get some sleep.

        1. As Megas are now little more than a fiver from China inc post, it makes sense. Good night (just left Node-Red alternating 2 lights for overnight to ensure this is reliable).

  4. Regarding linking an Arduino Uno or Mega there is the Jeelabs esp-link package that works well for linking an ESP to Arduinos. I have used this with some success.

    I have some of the those Chinese PCF8574 modules and because I already had a level shifter on the I2C lines I popped off the pull ups on the modules. They work a treat. I use one PCF8574 to provide a selectable board address and the second one to drive an 8 relay module. The opto coupled relays need active low to turn on and so the low output from the 8574 is perfect. There is a library for the ESP, “pcf8574_esp”.

    There is also a library for the MCP23017 (SPI) by Majenko that works on an Arduino but I haven’t yet tried it on an ESP8266

  5. Ok, done. I’m about to upload a modified set of ROMS and code with I2C commands that can include parameters and getting a byte back. I’ve also done Arduino code that can handle the larger chips…

    Up to now I’ve sent commands to set a port PWM or digital and to read a port analog or digital.

    All works spiffingly well, just need to decide on a couple of things like – should I use up 3 pins to set a range of adddresses – or have a single start address and program the address into a board (store in Arduino EPROM)… Hmmm.

    Will upload soon. This could be good…. The Arduino code is in the Arduino environment so one could add any kind of sensors – that now expands my Node-Red capabilities as slapping an ESP8266 and mega together opens up a whole new set of possibilities. The i2c part of all of this only takes up a couple of K or so. I can feel a blog entry coming on. Might have to wait until tomorrow as it’s gettting warm in here.

  6. Has anyone already hooked up a PIC or Arduino as an I2C slave to include not just basic I/O, but multiple ADC capability, too? I’d like to have enough ADC channels to handle “Solar voltage”, “Battery voltage” and “Battery current” (sound familiar to anyone :-)), all at reasonable resolution (and maybe provide an LCD status display and control buttons, later on) and have the ESP talk to ground-control from my not-too-remote, lead-acid charger and pump controller.
    The idea of using a small (cheap!) microcontroller to add functions via the I2C bus isn’t new, but it does fit well with this idea of extending the (fairly limited) capabilities of the ESP.
    I’ve started with a PIC and JAL, but hopefully this wheel doesn’t need re-inventing.

    1. No but I’ll tell you what I’m doing right now – I’ve just ported my line parser to the Arduino… ie {out2:1} etc…. there is a command in my code called EXT where you can send a command to an external device… I could have used Firmata but as I’m used to the commands now…..

      So up to now, the parser works – and I can turn a light on port2 (Atmega1284) on and off. It won’t be long before I can read and write all the ports – then PWM then analog in…..

      1. We’re both at pretty much the same stage, then. I’m sure that someone else out there has already done this as a project (just maybe not for the ESP, yet).

        1. How’s your I2c? See latest blog… WORKs – but not the way I’d like it to.. I’m missing something.

Comments are closed.