Simple Circuit to test Solid State Relays


Solid State Relays can be purchased mounted on boards from one to eight mounted on boards for the Arduino. I purchased a two relay board, like this one here. Currently they cost around two to three times a conventional relay mounted on a PCB. The advantages are that they are smaller, have no moving parts and are silent in operation.

I put together a simple circuit to test a two channel SSR, but it could be expand as required. The relay board  has a Ground Connection and a 5 volt supply pin. Each relay has a trigger pin. The Relay is switched on with a logic HIGH (5 volts).

The circuit I used is shown here:


And here is the sketch I used:

* Solid State Relay, Simple Test Circuit
* Connections:
* DC_ to Arduino 5 volts
* DC- to Arduino Gnd
* CH1 toArduino pin 2
* CH2 to Arduino pin 3
* Chris Rouse December 2016
#define CH1pin 2
#define CH2pin 3

void setup() {
pinMode(CH1pin, OUTPUT);
pinMode(CH2pin, OUTPUT);
digitalWrite(CH1pin, LOW); // turn CH1 OFF
digitalWrite(CH2pin, LOW); // turn CH2 OFF


void loop() {
digitalWrite(CH1pin, HIGH); // turn CH1 ON
digitalWrite(CH2pin, LOW); // turn CH2 OFF
digitalWrite(CH1pin, LOW); // turn CH1 OFF
digitalWrite(CH2pin, HIGH); // turn CH2 ON


The two relays are simply turned off and on and the LEDs light to show the relays are operational.


Magic Cup Module with Arduino


This is rather an odd module, I had seen them, but only purchased one recently. I assume they are designed to sit at the bottom of a cup and the LED lights up when the cup is tilted. There are four connectors marked +, -, S and L on my board. I decided to draw the circuit out to see how it would connect, the circuit diagram is shown below.


At first sight it seemed a little odd, I connected pin S and L together and connected the + to the Arduino 5 volt pin and the – to the Arduino Gnd pin. Tilt the module and the LED lights, tilt it back and the LED goes off, the LED is lit when the Mercury switch contact is BROKEN. I assume the reason is that the current drawn through the LED does not pass through the Mercury tilt switch. When the Mercury switch is closed the LED is shorted and a small current flows through the resistor and switch to Gnd. When the Mercury Switch is open the current flows through the resistor and LED to Gnd and it lights up.


This picture shows the mercury bead does not cover both contacts and the switch is OPEN.


This shows the contacts CLOSED as the Mercury covers both contacts and the LED is OFF.

The circuit does not really need the Arduino and will work happily with juts a 5 volt power supply. However, as both the switch and LED are available individually I wrote a short Sketch that allows the operation to be changed, or used in a much bigger project.

* Magic Cup Demonstration
* Chris Rouse December 2016
* Connections:
* + or Vcc to Arduino 5 volts
* – to Arduino Gnd
* S (Mercury Tilt Switch) to Arduino pin 8
* L (LED connection) to Arduino pin 7 via a 220R resistor
* Note: a 220R resistor must be used in series with the LED or it will be destroyed

#define mercuryTilt 8
#define LED 7
#define onBoardLed 13
boolean state = false;
boolean initialState;

void setup() {
pinMode(mercuryTilt, INPUT);
pinMode(onBoardLed, OUTPUT);
initialState = digitalRead(mercuryTilt);
digitalWrite(LED, LOW); // start with LED off

void loop() {
state = digitalRead(mercuryTilt);
if(state != initialState){
digitalWrite(LED, HIGH); // turn LED ON if switch has been tilted
digitalWrite(onBoardLed, HIGH); // echo output to Arduino’s LED
digitalWrite(LED, LOW);
digitalWrite(onBoardLed, LOW);

The sketch first determines which way up the tilt switch is and makes this the OFF position, when the switch is tilted the LED will be turned ON. to reverse this action change the line

if(state != initialState){


if(state == initialState){

The LED will be ON until the switch is tilted

I admit that this circuit could have been built using discreet components, but I purchased two of these modules for less than £1 on eBay, post paid.


Joystick Weather Clock, 15 months on


The original post for this project can be found here  A link to all the files on GitHub can be found here.

Following a suggestion from Alan Powell I decided to add a Humidity Sensor. The DHT11 is cheap and suited to this project. A lack of variable space meant that I was unable to save the data to the SD Card but as a bonus, the Humidity screen displays the Dew Point. See the end of this blog for more details.

Back in September 2015, I built this Joystick Weather Clock and it has sat on my desk recording pressure and temperature on an SD Card for fifteen months, without missing one hourly record. The data was stored as a CSV file so that it would load into EXCEL or similar spreadsheet program. I decided to see how it was faring after running all that time uninterrupted.

The fact that it was still saving data meant that there had been no stack overflow caused by unused variables filling up available free memory. The DS1307 Real time clock had not done too well, it was now 45 minutes fast, gaining around 38 seconds per week. The OLED display seemed to have lost a number of pixels and in places the display looked a little ragged, the LEDs used to light up under the display were still going strong. The pressure sensor was working perfectly.

So much for the hardware, what about the software? The calendar was perfect, having come through a leap year and the moon phase display was spot on. Data had been recorded exactly as designed and the whole file on the SD Card took up less than 4mb. Over 11,000 hourly temperature and pressure readings had been recorded and I believe this unit could run unattended for several years.

The pressure information was the most interesting, it was possible to follow the remains of a hurricane that hit the UK at the start of January 2016.


The pressure had remained fairly stable at around 102000 pascals until New Year’s Eve, dropping to below 98000 pascals over four days. The result was high winds and flooding.

I am really pleased with the software side, but I feel that a better Real Time Clock module is needed. To protect the OLED I think some kind of simple Screen Saver is needed and I will add this in the new year sometime. The subroutines to calculate the calendar and moon phase have worked flawlessly and, although the data saved on the SD Card is OK, I think that a VBA routine for EXCEL to extract the Pressure and/or the Temperature data and to place it all in a single column is needed. This will allow a graph to quickly be drawn. Please note that the hourly data builds up throughout the 24 hours and only the data saved at  2300 hours contains all the days’ data.

One final addition would be to use a 5-volt power bank as a backup battery, it can be left plugged into the Arduino and would only be used if the mains power was lost. The software saves the last 48 hours in a temporary file on the SD card, if the power is lost then once power is restored this temporary data is loaded, but if the power is lost for more than one hour the data for the time during power down would be lost.

Adding another Sensor:

The Sketch uses 74% of the available space for variables and at 78% usage the Arduino  IDE starts to complain, so I have tried to keep below this figure. I decided to try to fit a Humidity sensor, following a suggestion from Alan Powell. The DHT11 although slow is quite cheap and as long as you don’t request data too often it should work well. I started by installing the library and the routine to read the Humidity data. That consumed very little precious variable space. I then added a new screen to display the data. Now I had a working Humidity screen that also displayed the Dew Point.

The DHT11 is a slow device that should not be addressed more than once every few seconds. I decided to save the Humidity value and only update it once an hour when data is saved to the SD card. When you display the Humidity screen the value could be up to an hour out of date, but simply press the joystick and the value is updated immediately.

I fitted a ChronoDot board with a DS3231 RTC chip, this is just a simply plug in replacement, I just needed to swap the SDA and SCL lines. No change to Library or Sketch is needed.

Heres to the next 18 months when I can see how well the new RTC performs.


The new Humidity screen, accessed by moving the Joystick one place left from the Analog Clock Screen. The age of the reading is shown and although this is updated once an hour it can be updated manually at any time by pressing the joystick switch in this screen.

The DHT11 has three pins 5v, OUT and GND. Connect 5v and Gnd to the Arduino and OUT to digital pin 12. While I was fitting the DHT11 I took the opportunity to swap the DS1307 RTC for a (hopefully) more accurate DS3231.

The picture at the top of this post shows the new ChronoDot RTC and DHT11 fitted to the clock.


Use this connection diagram in conjunction with the wiring instructions in the sketch. Check the pinouts on your devices before wiring as they may not match those shown above. The OLED will die instantly if not connected correctly.