AT24C256 EEPROM Writing to a page at a time with Arduino


See the sketch on my GitHub page here.

The At24C246 EEPROM  can be used to add a few bytes of permanent memory to an Arduino project. Although there are a total of 256 bytes available, this is arranged in pages of 32 bytes  you cannot save a character across a page boundary. There is an excellent description and instructions for use on the Hobbytronics website here.

This memory can be used to store setting for a clock etc, that can be loaded into the Arduino as it powers up


Passive Buzzer for Arduino (Keyes KY006)

passive buzzer

Unlike the Active Buzzer, which generates a fixed tone when the power is connected, the passive needs a generated tone, as a loudspeaker does, to make it operate. This buzzer is almost identical in appearance to the Active Buzzer YL-44.

Either PWM or simply switching a digital pin on then off,  the frequency being determined by the length of the delay between switching on and off.

The second method is used here to generate a two tone signal.

Example Code for KY-006, Two Tone.

  Gnd to Arduino Gnd

  Vcc to Arduino 5 volts

  I/O to Arduino pin 6

int out =2;

void setup ()
  pinMode(out, OUTPUT);

void loop ()
  unsigned char i, j ;
  while (1){
    for (i = 0; i <80; i++){ // number of cycles to sound
      digitalWrite (out, HIGH); // turn buzzer ON
      delay (1) ;// Delay 1ms set frequency 1
      digitalWrite (out, LOW); // turn buzzer OFF
      delay (1) ;// delay ms
    for (i = 0; i <100; i++){ // number of cycles to sound
      digitalWrite (out, HIGH); // turn buzzer ON
      delay (2) ;// delay 2ms set frequency 2
      digitalWrite (out, LOW); // turn buzzer OFF
      delay (2) ;// delay 2ms

Flame detector with Arduino


The Keyes version, KY026, only has a digital output but the code shown will still work. It will show the presence of a flame, but not show the Analog output.

The Flame Detector works by sensing the Infra Red in the flame In the range 760 to 1100nm. Any IR controller, like that used for TV etc, will also operate the detector but the output will be variable as the controller sends a series of pulses rather than a continuous signal.

I have set the sketch so that the sensors four pins can be plugged into the first four Analog pins. A0 is the Analog input, A1 is the digital input. A2 supplies the Gnd and A3 supplies the 5 volts. Before using this sketch make sure your pin out matches mine. Also bear in mind that the KY026 only has three pins, Vcc, Gnd and DO, so will not plug into the Analog pins directly (use connecting leads).

Upload the sketch and plug in the board (check the connections!). Open up the Serial Monitor and the Analog reading should be shown. If the output shows Flame Detected the output from your board is opposite to mine. Check the sketch and unrem the two lines in void loop that will work with an opposite output (REM out the current two lines).

You can test the circuit with a match or lighter, the maximum range is about four to six inches. An infra red controller can be used but the output will be a bit unreliable, probably showing flame detected every five or six times.

I suspect these detectors are used to watch pilot lights in boilers etc, but their short detection range make them unsuitable as a fire detector.

Flame Detector
/*Sensor pin out

Analog out to Arduino pin
Digital out to Arduino pin
Gnd to Arduino Fnd
Vcc to Arduino 5 volts

#define flameDetect_An A0 // analog output
#define flameDetect_Di A1 // digital output
#define GND A2 // Gnd
#define Vcc A3 // 5 volts

int led = 13; // onboard led

void setup() {
pinMode(led, OUTPUT);
pinMode(flameDetect_Di, INPUT); // Digital input
pinMode(GND, OUTPUT);
pinMode(Vcc, OUTPUT);
digitalWrite(GND, LOW); // make pin A2 supply Gnd
digitalWrite(Vcc, HIGH); // make pin A£ supply 5 volts
void loop() {
Serial.print("Sensor Value: ");
Serial.print(1024 - analogRead(flameDetect_An));
if (digitalRead(flameDetect_Di) == LOW){
//if (digitalRead(flameDetect_Di) == HIGH){
Serial.println(" Flame Detected");
digitalWrite(led, HIGH);
digitalWrite(led, LOW);

Using an SD Card on an Arduino.

image image

These inexpensive SDCard boards are easy to use and there is plenty of information on the Arduino Playground and other places. From the circuit diagram it would seem that level conversion is needed to convert the 5volt signals from the Arduino to the 3volts needed by the SD card. The card itself can be powered from 5 volts. If you do Power from 5 volts, then do not connect the Arduino 3volt pin to the 3volt pin on the SD board.

The standard Arduino SD library works well although there are one or two others available. File name lengths are limited to eight characters plus a three character extension. In addition the library does not support the use of string variables in the commands, so for example

File file;
String filename = "test.txt";
file =;

will not work, and the following must be used

File file;
file ="test.txt");

must be used.

If you use your card in a data logging application and you remove the card while the sketch is running, the card may not be recognised when it is returned and data will not be read from or saved to the card. Pressing the Reset button will restart the sketch and re mount the SD Card.

Reading CSV files from an SD Card

Writing CSV files to an SD card is a fairly easy matter, build a string, adding a comma between each number and send that string to the SD card. However, getting that data off the card and loaded back into arrays to use is not such an easy matter.

My application read temperature and pressure each hour, storing the results in an array. Three days worth of data is stored on the Arduino to allow the data to be plotted on three graphs. To do this I needed two arrays each 3×25, ie SDtemperature[3][25] and SDpressure[3][25]. This data is stored once an hour as a CSV file that can be opened in a spreadsheet such as Excel. What I wanted was to replace the data held in strings if the power was lost, however, I did not have a lot of space available on the Arduino.

I spent several hours on the internet looking for for an answer and eventually found the method shown below. This sketch has been modified to suit my requirements and the original can be found here on the Arduino Forum. To save dynamic memory I reuse most of my variables and unlike the original sketch I did not want to print the data out, but to save it in the data arrays.

My sketch checks for the presence of the data file in the setup loop and if it exists loads the data into the arrays. Each hour the file is deleted and replaced by the latest readings, so unlike the main data file which grows each hour the backup file remains small. The only problem I found was that decimal numbers needed to be multiplied by 100, then divided by 100 when read back in.

Using this method allows saved data to be automatically restored in the event of power loss.

You can see this method being used in my blog for the Joystick Weather Clock here.

MAX6675 Thermocouple Shield and Arduino


a K type thermocouple is unlike other temperature sensors like thermistors and Digital Temperature sensors like the TMP36. A thermocouple is made by welding two wires made of Nickle and Chromium, hence NiChrom, to form a junction. A current proportional to the temperature is produced and this must be compared to a reference voltage called a cold junction.


The MAX6675 performs cold-junction compensation and digitizes the signal from a type-K thermocouple. The data is output in a 12-bit resolution, SPI™-compatible, read-only format.

This converter resolves temperatures to 0.25°C, allows readings as high as +1024°C, and exhibits thermocouple accuracy of 8 LSBs for temperatures ranging from 0°C to +700°C.

This code is from the Adafruit, but it is my intention to use an OLED for display and will publish this soon. The library can be downloaded from here.

// MAX6675 Thermocouple

#include "max6675.h"
int thermoDO = 4;
int thermoCS = 5;
int thermoCLK = 6;

MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);
int vccPin = 3;
int gndPin = 2;

void setup() {
pinMode(vccPin, OUTPUT); digitalWrite(vccPin, HIGH);
pinMode(gndPin, OUTPUT); digitalWrite(gndPin, LOW);
Serial.println("MAX6675 test");
// wait for MAX chip to stabilize

void loop() {

// basic readout test, just print the current temp
Serial.print("C = ");
Serial.print("F = ");

Writing CSV files to an SD Card with An Arduino.


The use of the SD Card reader is well documented on the Internet and I have found the sketch shown here to work well in a number of data logging projects I have built. This is just an outline sketch and should be modified to meet your needs.

I save the file in CSV format so that it can be imported into a spreadsheet program like Excel. The CSV format is just a text file containing data separated by ‘commas’ to produce ‘columns’ of data. A new ‘row’ is formed by adding a CR/LF character (this can be achieved by using file.println(datastring);

Writing Sensor Data to an SD card
This example shows how to write data
to an SD card using the SD library.

The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 10 Uno (53 on Mega)

Based on code by Tom Igoe
#include "SD.h"
//the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD
// library functions will not work.
const int CSpin = 10;
String dataString =""; // holds the data to be written to the SD card
float sensorReading1 = 0.00; // value read from your first sensor
float sensorReading2 = 0.00; // value read from your second sensor
float sensorReading3 = 0.00; // value read from your third sensor
File sensorData;
void setup()
// Open serial communications
Serial.print("Initializing SD card...");
pinMode(CSpin, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(CSpin)) {
Serial.println("Card failed, or not present");
// don't do anything more:
Serial.println("card initialized.");
void loop(){
// build the data string
dataString = String(sensorReading1) + "," + String(sensorReading2) + "," + String(sensorReading3); // convert to CSV
saveData(); // save to SD card
delay(60000); // delay before next write to SD Card, adjust as required
void saveData(){
if(SD.exists("data.csv")){ // check the card is still there
// now append new data file
sensorData ="data.csv", FILE_WRITE);
if (sensorData){
sensorData.close(); // close the file
Serial.println("Error writing to file !");