• Category Archives Automation
  • ATmega328 PLC, the best you can get


    The ATmega328 PLC is a credit card controller packed with many useful functions. It is light weight, consumes low power, has a DIN rail case, build in display, backup power and is scalable. With this PLC you can build and control anything you like. Compared with other PLC’s there is no need for special power supplies, or other interface modules. Practically any sensor from any manufacturer can be connected. On top of all this it is cheap compared with others PLC’s in the market.

    The picture below shows the PLC as monitor and measuring device of the electricity grid. This application can activate generators and backups at times when the CFE has blackouts. ( Grid values are zero because the photo is for demonstration purpose).

    The ATmega328 PLC


    The inside

    The PCB is a high quality double layer made of glass fibre, 1.5mm thick, routing is done with 10mil traces. The PCB measures 100×68 mm has 85 components, the enclosure measures roughly 107x88x60 mm.

    Below an image of the PCB layout and the various functions and interfaces.


    The ATmega328 PLC


    Interested to buy : send a message

    The idea behind it

    This PLC was developed for different reasons. One reason was to generate a flexible control platform with a display integrated to control any process you like. A second reason was to offer a complete integrated PLC solution to any one like students professionals and engineers. With this PLC you can focus on your job instead of being busy how to assemble and install a PLC controller.

    The basic idea regarding the specifications for the PLC was:

    • It had to be small in size with a low power consumption
    • Should run from solar panel and or standard Lithium or lead acid batteries.
    • It should have build in AC power drivers.
    • Had to be expandable or scalable.
    • Standardized Interfaces and there for sensor manufacturer independent.
    • Equipped with a large display
    • Memory to store statistic measurements.


    This controller platform has a number of applications. Some are available and some are in a concept phase of development. Holamex works on the following applications.

    • Photo voltaic energy monitor
    • Smart solar collector differential thermostat
    • Battery heath monitor and charger.
    • Swimming pool controller with automated pool and garden light timer
    • Load control for factories with high energy demand.
    • Light lux meter.
    • Flow meter
    • High way traffic density counter.
    • Smart Solar light post monitor and battery maintenance.
    • CFE grid monitor with backup switch functionality.
    • Air pollution monitor.


    The following functions are implemented

    • Central controller, ATmega328
    • RTC, Real Time Clock with high accuracy, DS3231,
    • Non volatile RAM, AT24C256, 64KB
    • RS485 half duplex bus, MAX485
    • ADC external reference, multi turn pot meter
    • Temperature sensors, DS3231, ATmega328
    • Push buttons.
    • LCD Display 4×20.
    • External Terminal port / PC interface

    Ports on the module

    The ports on the PLC allows connecting external sensors or measurement modules. Some of the ports provide beside the GPIO signals also the necessary power to feed the external modules or sensors. The ports are implemented with 3.81 mm pitch screw terminals. The following ports are available:

    General IO ports 2 GND, 5/12V, 2 IO’s
    Analog out port 1 GND, 5/12V, 2 12bit Analog out
    Relay port 1 NC or NO contact 8A, 250VAC
    DC out port 1 12VDC, 1ADC
    120V power port 1 Phase cutting 120VAC, 4A
    ICSP port 1 In Circuit Serial programming ICSP

  • Read and Write to EEPROM memory

    I was programming for a CFE network monitor to check on the quality of the electricity grid in Mexico

    This program and hardware can start up generators and electricity backups in case the grid is out of specifications or specified electric limits. It also will reconnect to the grid under controlled conditions. So this Grid Guard protects your house or business form mall functioning grid conditions.

    The “grid  guard”  has a display showing voltage current, Power, Energy,  Cos phi, frequency and network status.  More options are possible but that is planned for future releases.

    One of the functions I needed was to secure data in case of disconnecting or changing the software in the Grid guard. To do this  use the EEProm memory. The EEPROM.h library doesn’t have a general function that can write and update any desired variables to EEPROM. I wrote the two functions for this reason and you can use them without including the EEPROM.h libraries.

    After compiling by using the arduino IDE it indicated that the two functions use 444 byte of program memory and 9 byte of RAM.

    For the ones interested copy and past the code in your sketch and have fun.

    // This function is independent of data type. It copies all standard types but also string  //
    // and structs. This function reads [n] bytes from RAM pointed by ptr and writes them to    //
    // EEmemory at location EEaddress. This function checks if EEmemory contains the value to be// 
    // written. If it has it skips the programming section and runs for the next byte.          //
    void MEM2EE (unsigned int EEaddress, byte *ptr, unsigned int n) {
        n=n&0xFF    ;                        //safety statement can be removed
        while (n != 0) {
            while(EECR & (1<<EEPE));         // Wait for completion of previous write 
            EEAR  = EEaddress;               // Set up address register 
            EECR |= (1<<EERE);               // Start eeprom read by writing EERE 
            if (EEDR != *ptr) {              // skip write if ptr value is equal to EEmem
                EEDR  = *ptr;                // load data register with pointed byte
                EECR |= (1<<EEMPE);          // Write logical one to EEMPE
                EECR |= (1<<EEPE);           // Start eeprom write by setting EEPE 
    //      else Serial.println("EQD");      // test if data ptr and EEmem equal print msg
    // This function is independent of data type. It copies all standard types but also string  //
    // and structs. This function reads n bytes from EEaddress in EEmemory and writes these to  //
    // pointed location ptr in RAM.                                                                 //              
    void EE2MEM (unsigned int EEaddress, byte *ptr, unsigned int n) {
         while (n != 0) {
                while(EECR & (1<<EEPE));       // Wait for completion of previous write
                EEAR  = EEaddress;             // Set up address register
                EECR |= (1<<EERE);             // Start eeprom read by writing EERE
                *ptr  = EEDR;                  // Assign EEDR data to memory location

    Examples of how to use these functions.
    Assume you want to write to EEPROM memory addresses 540 and 542

    You have the following variables :
    float            fvar1=3.1416,       fvar2;
    byte            bvar1=66,              bvar2;

    These are the calls

    MEM2EE( 540, (byte*)&bvar1, sizeof(bvar1));
    EE2MEM( 540, (byte*)&bvar2, sizeof(bvar2));
    MEM2EE( 542, (byte*)&fvar1, sizeof(fvar1));
    EE2MEM( 542, (byte*)&fvar2, sizeof(fvar2));

    Your can write and read any variable, array or  struct in this way to and from EEPROM memory

    Write a comment if you like it or want to contribute to this code.