Zonnestroompanelen in Nederland

duurzaamheid achter de meter

Pulse generator with optocoupler

Arduino ‘pulse’ generator using a photo-transistor optocoupler

by Floris Wouterlood –  Leiden, The Netherlands – April 10, 2016

This paper describes the concept and construction of an Arduino-based pulse generator that mimics what a digital kWh meter does when power passes through. The design uses an optocoupling device to separate electric circuits.

The production of my solar panels is measured with a digital kilowatthour (kWh) meter which carries a led that gives a short flash for every half watt-hour of power passing through (2,000 pulses/kWh). Simultaneously with these flashes a so-called transistor optocoupled device inside the meter is activated for approximately 90 msec. This causes two contacts on the meter (the S0 port, contacts numbered 20 and 21) to be shorted. A voltage placed over the S0 port drops every time the led flashes. The sequence of S0 voltage drops during solar power production can be monitored by a microcontroller. This is exactly what I intend to do. For a safe workflow and to prevent frying the microcontroller a S0 port should electrically be completely isolated from the main power current. The manufacturer of my kWh meter guarantees this aspect. The S0 port  of my solar kWh meter operates with a DC current with voltages between 5 and 27V. A pin set ‘HIGH’ on an Arduino carries 5V DC. I therefore consider my digital kWh meter to be Arduino compatible!
This project is the first step in a broader scheme aiming at constructing a pulse counter based on the Arduino platform. Reason for desiring a pulse generator is that the solar kWh meter pulses only during daytime. Testing and developing is done mostly in the evenings, that is after sunset. No solar production then, and no pulses! A reliable source of pulses was necessary to start building and testing the pulse counter. I needed a pulse generator that mimics a kWh meter’s S0 port at full operation.

Figure 1 shows the connections between solar panels, inverter, wall outlet and Arduino.

Figure 1: This is what I intend: counting pulses with an Arduino via the S0 port of the ‘solar production’ kWh meter.

Electronics needed
Arduino mircocontrolelr board, 4N35 optocoupler

Concept of pulse generation
Digital pins on an Arduino can be programmed HIGH, that is that in this state they will carry a 5V voltage against GND. If we set, let’s say, pin 2, HIGH, connect this pin with the S0 port of the kWh meter and complete the circuit by connecting the other contact on the S0 port with GND, then the voltage on the Arduino’s pin 2 will drop as soon as the kWh meter pulses. Activity of the SO port of the kWh meter can be mimicked by an optocoupler.

The 4N35 is a cheap transistor optocoupler perfect for use with an Arduino. It has three pins on the primary side, marked 1,2 and 3. Pin nr. 1  is connected to a suitable GPIO pin on the Arduino, nr. 2 is connected to GND, while pin 3 is not connected. Of the three pins on the secondary side, pins 4 and 5 are the switched pins that we need in the secondary circuit (i.e., the S0 port imitation)

The connectivity scheme for the 4N35 optocoupling device is presented below.

Figure 2: 4N35 optocoupler: connectivity and implementation. A 220Ω resistor between 4N35 and the Nano  is compulsory because the 4N35 contains an internal 3V led.

An optocoupler works as follows
Inside a powered optocoupler a led produces infrared light. A transistor-like photo-sensitive device detects the infrared light and closes a switch in the secondary circuit. The entire assembly is enclosed in a light-tight package with metal legs for the electrical connections. Optocouplers are small; the picturer in figure 2 was taken with a x5 magnifying digital microscope. Note that the primary and secondary pins are electrically separated.
In the sketch we have selected pin 4 on the Arduino for pulse generation, but any free digital pin will do. Note that the 220Ω resistor must be present to reduce voltage because the 4N35 is a 3V device.
As soon as we apply a 3V voltage on pin 1 of the optocoupler, pins 4 and 5 of the optocoupler are short circuited, thus mimicking the S0 port on a digital kWh meter. Now we have a working pulse generator!
A yellow led was attached to pin 8 of the Nano to optically report a pulse being ‘fired’. This is only to have a visual cue that pulses are actually generated.


// pulse_generator
// via optocoupler 4N35
// Floris Wouterlood April 10, 2016
// Public domain

// optocoupler attached to pin 4
// control led attached to pin 8

//one 60 msec pulse every 3 seconds

// about the 4N35 type optocoupler:
// dot left lower corner
// left to right:
// bottom row pins: 1-2-3
// top row pins 6-5-4
// pin 1 of 4N35 to pin 4 on Arduino – with 220 ohm resistor in between!
// pin 2 of 4N35 is GND
// when pin 1 on 4N35 is set HIGH, then pins 4 and 5 on 4N35 are short circuited: switch closed

long p=0;                                      // counter of number of pulses generated
int optoswitch_delay=60;
int interpulse_delay=2940;

void setup() {

// initialize digital pins.
pinMode(8, OUTPUT);                  // yellow on-off monitoring LED
pinMode(4, OUTPUT);                  // optocoupler switch pin on the Nano
Serial.begin (9600);                      // start serial monitor

void loop() {

delay(interpulse_delay);                // interpulse period
digitalWrite(8, HIGH);                     // turn the yellow LED on
digitalWrite(8, LOW);                     // turn the yellow LED off

digitalWrite(4, HIGH);                     // open the optocoupler
delay(optoswitch_delay);              // wait
digitalWrite(4, LOW);                     // close the optocoupler

p=p+1;                                             // increase counter



Actually this construction is not a pulse generator per se, just as a digital kWh meter does not actually deliver a ‘pulse’ on its S0 port but only shorts contacts 20 and 21 when the indicator led flashes. During activation by the  Arduino there is no active voltage on the secondary side of the optocoupling device, The only action happening is that pins 4 and 5 of the optocoupler are internally connected during the ‘pulse’ period. A second microcontroller board, connected to the secondary pins of the 4N35, is necessary to provide a voltage across the S0 port and to register the kWh meter’s pulses. Such a configuration will be the subject of the next paper in this series.

A ‘pulse’ triggered by an Arduino in an optocoupler just shorts two secondary pins on the optocoupler, just as it happens in a digital kWh meter. We need a) a voltage across the optocouplers secondary pins (that will drop each time the optocoupler is triggered) and b) a device that notices these voltage drops. An Arduino board supports ‘interrupts’ on its pins 2 (INT 0) and 3 (INT1). An interrupt is exactly what is says: the normal program routine is interrupted to do something specific when an event happens on a pin designed to notice interrupt events. Both a drop or a rise in voltage can be programmed to trigger an interrupt. Life would be simple if we could use one and the same Arduino that both  notices pulses, counts them and displays them. This is indeed possible, e.g. with a LCD display, if not I wanted to display the count number on a 320×480 TFT screen. The wiring from the Arduino to these screens ‘occupies” pins 2 and 3 of the microcontroller board necessary for monitoring interrupts. Apart from the Arduino po\wered pulse generator the solution is to use two Arduinos: one (called a ‘slave’) that is connected to pulse generator and which counts interrupts and via the serial port tells the second or ‘master’ Arduino how many pulses have been counted. The master Arduino then displays the counts on the TFT screen.
In the final configuration, the pulse generator is disconnected and the ‘slave’ Arduino connected to the S0 port of the solar kWh meter.
Anyway, the current exercise produced a nice pulse generator that can be used for many purpose that needs a pulse through a switch being closed.

ref: http://www.electronics-tutorials.ws/blog/optocoupler.html