Posted on Leave a comment

Adding Piezzo speaker to Analog Output

In my previous post, I was using my LED matrix circuit to display the Fibonacci clock. So I am a bit stretched on the I/O port. And my son asked me, whether I can add a buzzer to beep every 10 seconds so that he can do “plank” exercise. So I found my old Piezzo speaker.

Piezzo Speaker

Because I am running out of port, I am trying to use the Digital Pin 1. Pin one is labelled as TX, so the speaker beeps every second the clock updates. It also makes noise when you loaded the code to Arduino. What I had done is just combining the code from Mario speaker song to play when the button is being pressed on the Fibonacci clock.

It doesn’t work well when I am connecting the piezzo speaker to Digital pin 1. So I go on researching whether I can connect the speaker to Analog pin. Apparently you can program the Analog pin to behave like a digital pin.

Here are the initialisation code:

const int speakerPinMelody =  A0;   // using Analog pin A0 as output

void setup() {
...
pinMode(speakerPinMelody, OUTPUT); // to initialise the analog output
...
}

Once the initialisation is done, I can use the same code to play the melody as used in the Super Mario speaker song circuit.

void playSong(int theSong[], int theTempo[], int arraySize) {
// iterate over the notes of the melody:
int totalNotes = arraySize;
Serial.println("Total note:" + String(totalNotes));
for (int thisNote = 0; thisNote < totalNotes; thisNote++) {

// to calculate the note duration, take one second divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000 / theTempo[thisNote];
tone(speakerPinMelody, theSong[thisNote], noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
Serial.println("Delay:" + String(pauseBetweenNotes));
delay(pauseBetweenNotes);
Serial.println("Stop");
// stop the tone playing:
noTone(speakerPinMelody);
}

delay(10);
Serial.println("Final Delay");
}

I am using a 1K resistor to connect one end of the piezzo speaker (red wire) to pin A0 and the other end of the speaker (black wire) is connected to the GND.

I omitted the rest of the LED Matrix circuit for simplicity. When the button is pressed the circuit will display a random dice face (1-6) on the LED matrix and then it will play the Super Mario song.

The result can be seen in the following youtube video. I hope you enjoy this post. Please leave a comment if you like this post or subscribe for frequent update on more simple circuits with Arduino. You can also send me an email if you would like the full source code.

Posted on Leave a comment

UniFi network controller failed to start

I was helping a friend to fix his UniFi network controller that failed to start. I didn’t know much about it, and we think this is related to the database mongodb which is full with logs of the events which make the size bloated up.

Here are the steps that I did to fix it.

  1. Stop the Unifi Service using the following command
service unifi stop

2. Repair the database with the following command

mongod --dbpath /usr/lib/unifi/data/db --smallfiles --logpath /usr/lib/unifi/logs/server.log --repair

3. Download the pruning script using WGET

wget https://ubnt.zendesk.com/hc/article_attachments/115024095828/mongo_prune_js.js

4. Perform the test run using the following command

mongo --port 27117 < mongo_prune_js.js

5. If the mongo db is not running you might have to run the following command first

sudo mongod --dbpath /usr/lib/unifi/data/db

6. Once the test run is success you will need to edit mongo_prune_js.js to disable “dryrun”, change the line var dryrun=false;

nano mongo_prune_js.js

7. Prune the database by executing the following command, this will take a while to run depends on the size of events that you have. I had about 2 millions events.

mongo --port 27117 < mongo_prune_js.js

8. If all goes well you should see a couple of OK message like the following

{ "ok" : 1}

9. Change the db and log files permission using the following (NOTE : this is an important step)

chown -R unifi:unifi /usr/lib/unifi/data/db/
chown -R unifi:unifi /usr/lib/unifi/logs/server.log

10. Finally start the unifi service using the following

service unifi start

11. if all goes well you should be able to login to the unifi web interface now.

Posted on Leave a comment

LED Matrix Fibonacci Clock

I was inspired by all the Fibonacci clock out there, so I decided to design one my own. I am using the same LED Matrix circuit as described in earlier blog post.

So the idea is to show the Hours and Minute with the maximum of 12, it is behaving like the hour hand and minute hand displaying the time. So the left “Fibonacci” pattern is used to display the hour and the right Fibonacci pattern is used to display the minute.

I thought of displaying the seconds later, so that the clock seems alive, and the seconds is displayed using binary like the Binary clock in the previous post.

With the concept done, we are now ready to modify the Arduino code. But before that let’s have a look at how the pattern will be displayed in the following picture.

So the biggest challenge is to get the pattern combination right. To make sure the digit is showing correctly for each seconds, we need to make sure the data displayed correctly.

If you look at the output bytes, that is how it need to display to get the exact pattern as what is shown. For the seconds is showing in Binary we need to superimpose that on the pattern here. This is shown in the following code:

void display_seconds()
{
int sec_one = _second % 10;
int sec_ten = _second / 10;
output[0] = (sec_ten & 0x01) << 7 | (sec_one & 0x01) << 6 | (output[0] & 0x1F); // saved the previous minutes
output[1] = (sec_ten & 0x02) << 6 | (sec_one & 0x02) << 5 | (output[1] & 0x07);
output[2] = (sec_ten & 0x04) << 5 | (sec_one & 0x04) << 4 | (output[2] & 0x03);
output[3] = (sec_ten & 0x08) << 4 | (sec_one & 0x08) << 3 | (output[3] & 0x09);
}

As you can see to show the Seconds we need to break up the seconds into “one” and “ten” digit this is done in the first 2 line by dividing and modding the seconds with 10. Then the LSB will be shown in Output[0], and so on. Because the LED matrix LSB is at the bottom, we need to shift the digit 7 times for the “ten” to be displayed at the top, and the seconds will need to be shift 6 times to get it to the second row. Then we need to super impose the minute digit which is mask with Hexadecimal of 0x1F(the highest number for Output[0]). Thus the following code:

output[0] = (sec_ten & 0x01) << 7 | (sec_one & 0x01) << 6 | (output[0] & 0x1F);

The second bit is masked using 0x02 and because it is the second digit we only need to shift it 6 times for the “ten” and 5 times for the “one” and then masking the minute by 0x07 (the highest number for Output[1]). Hence the following code:

output[1] = (sec_ten & 0x02) << 6 | (sec_one & 0x02) << 5 | (output[1] & 0x07);

And so on until the last second digit, and hopefully you get the idea of the pattern.

The next challenge is to display the minute. To do that we need to understand the fibonacci number pattern first. We only use the first couple of numbers

1,1,2,3,5

Given the fibonacci numbers, number 1 can be represented either by the first 1 or the second 1 in the sequence, so we do a random number generator to choose which LED to light up. For number 2 can be represented by 1+1 or just 2, we also use the random number generator to choose which pattern to light up. Number 3 can be displayed as 2+1 (first), 2+1 (second) or just 3, this is also done using random number generator to choose which combination to light up. Continue this pattern until number 12 which need to light up 5+3+2+1+1. In simple terms, to read the minute we only need to count how many lights light up on the “right hand” corner. This is done in the following code:

void display_minute()
 {
   switch(_minute/5)
   {
     case 1:
       switch(random(2))
       {
         case 0:
           output[3] = 1; break;
         case 1:
           output[4] = 1; break;
  } break;
case 2:
  switch(random(2))
  {
    case 0:
      output[2] = 3; break;
    case 1:
      output[3] = 1;
      output[4] = 1; break;

  } break;
case 3:
  switch(random(3))
  {
    case 0:
      output[1] = 7; break;
    case 1:
      output[2] = 3;
      output[3] = 1; break;
    case 2:
      output[2] = 3;
      output[4] = 1; break;
  } break;
case 4:
  switch(random(3))
  {
    case 0:
      output[1] = 7;
      output[3] = 1; break;
    case 1:
      output[1] = 7;
      output[4] = 1; break;
    case 2:
      output[2] = 3;
      output[3] = 1;
      output[4] = 1; break;
  } break;
case 5:
  switch(random(3))
  {
    case 0:
      output[0] = 0x1F; break;
    case 1:
      output[1] = 7;
      output[2] = 3; break;
    case 2:
      output[2] = 3;
      output[3] = 1;
      output[4] = 1; break; 
  } break;
case 6:
  switch(random(4))
  {
    case 0:
      output[0] = 0x1F;
      output[3] = 1; break;
    case 1:
      output[0] = 0x1F;
      output[4] = 1; break;
    case 2:
      output[1] = 7;
      output[2] = 3;
      output[3] = 1; break;
    case 3:
      output[1] = 7;
      output[2] = 3;
      output[4] = 1; break;
  } break;
case 7:
  switch(random(3))
  {
    case 0:
      output[0] = 0x1F;
      output[2] = 3; break;
    case 1:
      output[0] = 0x1F;
      output[3] = 1;
      output[4] = 1; break;
    case 2:
      output[1] = 7;
      output[2] = 3;
      output[3] = 1;
      output[4] = 1; break;
  } break;
case 8:
  switch(random(3))
  {
    case 0:
      output[0] = 0x1F;
      output[1] = 7; break;
    case 1:
      output[0] = 0x1F;
      output[2] = 3;
      output[3] = 1; break;
    case 2:
      output[0] = 0x1F;
      output[2] = 3;
      output[4] = 1; break;
  } break;
case 9:
  switch(random(2))
  {
    case 0:
      output[0] = 0x1F;
      output[1] = 7;
      output[3] = 1; break;
    case 1:
      output[0] = 0x1F;
      output[1] = 7;
      output[4] = 1; break;
  } break;
case 10:
  switch(random(2))
  {
    case 0:
      output[0] = 0x1F;
      output[1] = 7;
      output[2] = 3; break;
    case 1:
      output[0] = 0x1F;
      output[1] = 7;
      output[3] = 1;
      output[4] = 1; break;
  } break;
case 11:
  switch(random(2))
  {
    case 0:
      output[0] = 0x1F;
      output[1] = 7;
      output[2] = 3; 
      output[3] = 1; break;
    case 1:
      output[0] = 0x1F;
      output[1] = 7;
      output[2] = 3;
      output[4] = 1; break;        
  } break;
case 12:
      output[0] = 0x1F;
      output[1] = 7;
      output[2] = 3; 
      output[3] = 1; 
      output[4] = 1; break;
}
}

We done the same with the top left corner of Fibonacci sequence to display the hour. However the one tricky bit is Output[3] and Output[4] is shared between hour and minute, so we need to mask the minute so that we didn’t lost it. This is done using the following code:

output[3] = 8 | (output[3] & 0x01);
output[4] = 8 | (output[4] & 0x01);

Then the output is combined and displayed. The full source code can be downloaded in the download section of shop. If you like this, please leave a comment or share it.

So the final result you can see in the following video:

Posted on Leave a comment

Connecting Touch sensor to Bluetooth Amplifier

TTP223 Touch sensor

First of all, I would like to wish everyone a Happy New year of 2019, this will be my first post of 2019. I hope you had a great start of the year.

I had discovered a capacitive touch sensor TTP223 which simulate a switch function by “touch”, it is using the capacitive of your hand or any other organic matter to allow the switch function. It can be configured to as “Toggle” or “On/Off”.

I recently post a review about various Bluetooth Amplifier that I had been testing. And the CSR8635 Dual 5W Bluetooth Amplifier had a particular interesting feature where you can “Skip” tracks, “Vol+” Vol-“, “Play/Pause” which also double up as “Pick up” call function. The problem with this is the Switch needs to connect each function to the “common” connection. So I am trying to experiment whether this will work using the touch sensor above.

Touch Sensor and Bluetooth Amplifier

The picture on the left shows how the functionality of the touch sensor. When the touch area is being touch, it will connect the I/O pin to the Vcc. While the Bluetooth Amplifier switch needs to connect the “Next” pin to the “common” pin. After some research the common pin is connected to Vcc. So I wonder whether I can connect the I/O pin straight away to the “Next” pin. So I had experimented using the circuit below, and it seems to work ok.

Connecting the Touch Sensor to Bluetooth Amplifier control pins

So I had connected a 10K ohm resistor to limit the current that flow from Vcc of the touch sensor to the Bluetooth amplifier. It seems to be working ok at this stage. I had connected 2 pin, the “Next” pin and “Play/pause” pin. Both seems to work ok.

You can see the final connection in the following picture.

Touch sensors connected to Bluetooth Amplifier controls

If you like this, please let me know or subscribe to the blog to receive frequent tips on simple circuits.

Posted on Leave a comment

Bluetooth Amplifier Review

Finally all my Bluetooth Amplifier arrived. So naturally I started to tinker with them. Started with the following

“Bluetooth 4.0 Digital 2.1 Class D HIFI Power Amplifier”

Bluetooth 4.0 Digital 2.1 Class D HIFI Power AmplifierThis is so far supposed to be the loudest amplifier boasting the following feature:

  • 100W subwoofer
  • Left and Right channel of 50W each.

I am rummaging through my junk box trying to find a power supply that can supply recommended 24V with 5A rating. I had none, I probably had one in the garage, so I opted using my trusted modified PC power supply which can only supply up to 11V. Bluetooth module works ok, I can connect using my iPhone, but trying to connect one of the speaker using my 8 ohm speaker doesn’t produce any sound. Well I will need to figure that out later.

Over the weekend I get my speaker out, it is a Mordaunt speaker which is capable of handling up to 100W. Once I connect everything, there is no sound coming out of the speaker, only after a few seconds later the sound came out. It seems that the circuit had some sort of protection for the speaker. The sound is quite clear and crisp. I will need to make the video recording in youtube and posted later.

Second one on the list is “TDA7492P Bluetooth 4.0 Digital amplifier”

TDA7492P 50W+50W Bluetooth
TDA7492P 50W+50W Bluetooth

This is boasting to be capable of output 50W+50W. So I connected this to 9V battery. The Bluetooth connect seamlessly and the “Play”, “Pause”, “Next” and “Previous” button works seamlessly with Music app in iPhone and Spotify. I connect my 8 Ohm speaker to one of the output, and it produce a sound and then it stopped. I am not sure what is going on, so after looking at the speaker, one of the wire came off, so I had to solder it back, but still no luck with the speaker sound. The next test is to plug in the headphones, this works brilliantly, so I can test the “vol +” and “Vol -” button, this seems to work ok too.

When I try to connect my Mordaunt speaker to it second time around, the bluetooth light doesn’t came up any more, so I can’t connect the iPhone and play any music. It might had been damaged, so I am contacting the seller at the moment, and I will post back later with the result.

The third one in the list is “CSR8635 Dual 5W Bluetooth 4.0”

CSR8635 Dual 5W BluetoothThis board is small but quite powerful, it had all the bells and whistle of the second board (TDA7492P). It also have the microphone connector which the second board doesn’t have. The board have the connection for “Play”, “Next”, “Prev”, “Vol+”, “Vol-“, “Mute” buttons. Given the size you will have to connect all the buttons yourself depends on what sort of speaker you are trying to build. You don’t have to necessary connect all the buttons.

I connected one of the speaker output to my Sony surround sound speaker. I connect it to the middle speaker. The connect the power supply to +5V. The red bluetooth light light up to allow connection. Again I use the Spotify app on my iPhone, and it connect seamlessly. The sound is quite crisp and clear.

Posted on Leave a comment

Arduino Binary Clock

This post is build on top of the LED matrix in the earlier post. I always wanted to build an Binary clock, and since I had the LED matrix ready, and I found a concept done by other people in their project, I had an idea to re-use my LED matrix and turn it into a binary clock.

Here are the list of material that you will need:

  • Arduino uno/nano
  • Realtime clock DS3231
  • Button cell battery CR2032
  • 8×8 LED matrix
  • 74HC595N shift register
  • Prototyping board or breadboard

Step 1. The Design


Binary Clock
The LED matrix consist of 8×8 LED, so we design the top row to be YYMMDD with a space of empty column between the Year and Month and between Month and Date.

The hour is being displayed at the bottom row in the format of HHMMSS. We also allow a space of empty column in between hour and minute and between minute and second.

The Least significant bit is at the bottom and the Most significant bit is at the top. Each digit is being displayed using four LED, acting like BCD (Binary Coded Decimal) code, which is able to count up to 10. However the hour will only count up to 24 and both the minute and seconds will only count up to 59.

 

 

 

Step 2. The wiring diagram


Leveraging the same circuit to connect the LED matrix to Arduino, we only need to at the RTC (Real Time Clock) DS3231 to the Arduino. To do this we are connecting the SCL line from the DS3231 module to SCL line of Arduino and connecting the SDA line from the DS3231 to SDA line of Arduino. Once this is done we then connect the VCC line to 5V and the ground to Arduino GND.

The detail explanation of the other part of the circuit can be found in my previous instructables post related to 8×8 Matrix Letter game. If you would like to know more details please visit the linked page, you can also asked me if you have any questions related to the connection.

Step 3. Loading the Code

Once this is done, we can start to load the code from the Arduino interface. To initialise the RTC module we need to include the following library.

#include <Time.h>
#include <TimeLib.h>
#include <DS3231.h>

Then to initialise the clock for the first time, add the following code in the setup section of the code.

// Set sketch compiling time
clock.setDateTime(__DATE__, __TIME__);

The above initialisation is to make sure that the DS3231 is sync with the current date and time the first time it is being executed. As long as you had the button cell battery in the RTC clock, it will keep maintaining the clock, and you don’t have to re-initialise it every time.

The following code is the key to displaying the correct binary output:

int day_one = _day % 10;
int day_ten = _day / 10;
int month_one = _month % 10;
int month_ten = _month / 10;
int year_one = _year % 10;
int year_ten = (_year / 10) % 10;
int sec_one = _second % 10;
int sec_ten = _second / 10;
int min_one = _minute % 10;
int min_ten = _minute / 10;
int hour_one = _hour % 10;
int hour_ten = _hour / 10;

We need to separate the one digit and the ten digit of the day, month, year, hour, minute and second by using the division and the mod to get the remainder. Then the output is combined together before being pushed out to the LED matrix. This is done in the same procedure called display_time() as below:

output[0] = day_one << 4 | sec_one;
output[1] = day_ten << 4 | sec_ten;
output[2] = 0;
output[3] = month_one << 4 | min_one;
output[4] = month_ten << 4 | min_ten;
output[5] = 0;
output[6] = year_one << 4 | hour_one;
output[7] = year_ten << 4 | hour_ten;

The day digit is being combined with the second digit, the month digit is being combined with the minutes digit and the year digit is being combined with the hour digit. once this is done we then called the procedure display_char(output) to display all the combination into the LED matrix.

Final Thoughts

You can see the realtime clock in action in the following video.

You can download the full source code in the following location. If you like this post, please leave a comment and also don’t forget to subscribe to receive frequent update and more similar projects like this one. You can easily expanded this project to be able to display Fibonacci clock. I have some concept in my mind already, so when I had the chance I will make a small modification to be able to display the Fibonacci clock.

Posted on Leave a comment

Simple Frankenbot with lights and sound effect

Frankenbot

I was inspired by the cute Frankenbot papercraft from Instructables, so I thought why not make it truly alive by giving it some blinking lights and sounds effect. Here is the plan:

– Connect 2 LEDs for the eyes (different colours of course) and only one eye can be on at a time

– Connect 3 LEDs to the chest so that it can run lights left and right

– Play some scary halloween music for the full effect.

So here goes my Frankenbot ready for Halloween.

Step 1. Create the Frankenbot papercraft using the instruction from Instructables.

Step 2. Cut out the eye holes and insert LEDs

Franken bot eyes

 

I use the recycle LEDs that was joined together with one 220 ohm resistor. Different legs was connected to the same resistor so only one eye will be on at a time, and you will have to switch the polarity to light up the second eye.

Step 3. Prepare the chest holes for LEDs

Chest holesCut out 3 more holes on the chest. It is much easier to cut it out when you had the robot chest disassembled.

Step 4. Prepare the electronics

I am using the recycled electronics from the “Light Activated MP3 player” from the earlier post. You can follow the link to find the instruction on how to build one.

Things to note, the way the light activated will need to be reversed, in this we want the Frankenbot to stay idle when there is sufficient light detected by the sensor, and when the light is off, it will came alive blinking it’s eyes and the chest lights and also playing the horror music at the background. So minor modification to the source code is necessary.

Step 5. Connecting the LEDs

Connecting LEDs

What I had done here is essentially as follows:

– Eyes is connected through pin D2 and D6

– Chest LEDs is connected through pin D3, D4 and D5

So the following additional modification to the code is necessary

const int LDRpin = A0;       // LDR connected to pin A0 const int EyeCommon = 6;    // low = off
const int Eyepin = 2;      // Eye LED pin D23
const int Chest1 = 3;
const int Chest2 = 4;
const int Chest3 = 5;

Step 6. It’s Alive

FrankenbotThere are several final modification to the code is necessary as follows:

In the setup section add the following:

pinMode(LDRpin, INPUT);     // setup LDR
  pinMode(EyeCommon, OUTPUT);
  pinMode(Eyepin, OUTPUT);
  pinMode(Chest1, OUTPUT);
  pinMode(Chest2, OUTPUT);
  pinMode(Chest3, OUTPUT);
mp3.playFolderTrack(7,Song);

in the loop section add the following:

if (ldrStatus <300){
    //digitalWrite(LED_BUILTIN, LOW);
    digitalWrite(EyeCommon, LOW);  // eye LED had different polarity so that 
    digitalWrite(Eyepin, HIGH);	   // only one eyes can be on at one time
    delay(100);
    digitalWrite(EyeCommon, HIGH);
    digitalWrite(Eyepin, LOW);
    timeoutTimer ++;
    //Serial.print("LED Light");
    if (DEBUG) Serial.println(timeoutTimer);
    mp3.start();
    digitalWrite(Chest1, LOW); // chest LED had common cable connected, 	 	
    digitalWrite(Chest2, HIGH); // my common cable is connected to Anode, 
    digitalWrite(Chest3, HIGH);  // so EyeCommon needs to be HIGH
    delay(100);
    digitalWrite(Chest1, HIGH);
    digitalWrite(Chest2, LOW);
    digitalWrite(Chest3, HIGH);
    delay(100);
    digitalWrite(Chest1, HIGH);
    digitalWrite(Chest2, HIGH);
    digitalWrite(Chest3, LOW);
    delay(100);
    digitalWrite(Chest1, HIGH);
    digitalWrite(Chest2, LOW);
    digitalWrite(Chest3, HIGH);
    delay(100);
    
    onStatus = true;
    if (timeoutTimer > 200) {
      if (DEBUG) Serial.println(timeoutTimer);
      timeoutTimer = 0;
      resetCount = 0;
    } 
  }

The section above is when the light intensity is low, so we flicker the eyes LED and the chest LEDs. The following section is when the light is high, we only have one eyes turned on.

} else {
    digitalWrite(EyeCommon, LOW);
    digitalWrite(Eyepin, HIGH);
    digitalWrite(Chest1, HIGH);
    digitalWrite(Chest2, HIGH);
    digitalWrite(Chest3, HIGH);    

    mp3.pause();
    timeoutTimer ++;
 }

And don’t forget to upload the scary scene music to the sd card. In this example I put it in the folder “07” and the song is called “001.mp3”

Now upload the code and test it, if all goes well, when you flick the switch your Frankenbot will come alive. Cover the sensor or turn off the lights, it’s eyes should flicker and the chest LED will flicker too.

Enjoy and Happy Halloween!!!

If you like this you can subscribe for frequent update and leave some comments.

Posted on Leave a comment

IoT Gas Sensor with ESP8266

I had been wanting to get the gas sensor working with my ESP8266 as an additional item on the IoT list. So similar to the IoT Temperature sensor from the previous post, I had created the IoT gas sensor using ESP8266.

Using the same idea you don’t have to open port in your router, but leveraging an intermediate layer by publishing the data to a website. Here are the list of components used:

The circuit

We are connecting the Gas sensor analog output to Analog input (AD0) of ESP8266 as shown in the following picture.

ESP8266WithGasSensor

The Buzzer is connected to Pin GND and D3.

We will check the sensor via analog to give much better variation, we can also use the Digital output from the sensor, but you will have to calibrate this properly to ensure that it gives the desired trigger when a certain composition of gas was detected. You can use the prototype board to connect them all up and using Arduino interface you can load the code.

If you are not familiar on how to setup the Arduino IDE (integrated development Environment) I had an instructables post that can help you to get started.

The finished connection should look like something below:

IoT Gas Sensor with ESP8266

Now that you have a setup the circuit, there is one more step to setup the Cloud webserver (optional) if you want the sensor to periodically upload the gas reading to the webserver so that you can monitor this remotely.

Assumption: You have a working webserver, and familiar with transferring files to your webserver.

Web server setup

Transfer the content of “IoTGasSensorwebserver.zip” into the root of your webserver. Let’s assume that your webserver is “http://arduinotestbed.com”

You can put the file in a separate folder, but you need to modify the sketch to point to the correct location of “gasdata_store.php”

In this example we are assuming that the full path to the file are http://arduinotestbed.com/gasdata_store.php

Once you had uploaded the files, you can test that it is working going to the following link

http://arduinotestbed.com/GasData.php

watch out for the character case as most webserver are case sensitive. If all is working, you should see something that looks like the following:

IoT Gas Data

One more thing you will need to make sure is the file “gas.txt” needs to be writeable, so you need to set the permission of this file to “666” by using the following unix command:

chmod 666 gas.txt

This file is where the sensor data will be uploaded into by the ESP8266.

You can download the webserver scripting using this link.

Uploading the arduino sketch

Now you are ready to upload the sketch code to the ESP8266. You should have 2 files:

  • ESP8266GasSensor.ino
  • mainPage.h
  • settings.h

Put them all in the same folder and open the “ESP8266GasSensor.ino” in the Arduino IDE, then make small modification to the code to point to the correct webserver location shown in the following picture:

Arduino code changes

Modify the line

String weburi = "/gasdata_store.php"

if you didn’t put this file in the root of the webserver, this should point to the correct path of the file. Then compiled and load the sketch to ESP8266. If all goes well you should have a working AP (Access point) from the ESP8266 the first time you run this. The AP’s name is called “ESP-GasSensor”.

Try to connect to this AP using your laptop or mobile phone, then find out what is the ip address that was assigned to you, this can be done using “ipconfig” command in windows or “ifconfig” command if you are in linux or mac. If you are using iPhone you can click on the “i” button next to the ESP-GasSensor that you are connected to.

Open your web browser and point to the ESP-GasSensor Ip address, if you are assigned with 192.168.4.10 as your up, the ESP-GasSensor is have the ip of 192.168.4.1, so you can point your web browser to http://192.168.4.1

You should be presented with the settings page where you can enter your wifi configuration. Once you had enter your WiFi access point that connects to the internet, tick the “update Wifi Config” check box, and click on “update” to save the settings to the ESP8266.

The ESP8266 will now restart and trying to connect to your WiFi router. If all goes well you should see the gas data being updated to you webserver on a regular interval. In this example you can point your browser to

http://arduinotestbed.com/GasData.php

Congratulations!! if you manage to reach this part. You should give yourself a pat on the back. Now you can tell your friends about the gas sensor that you have.

You can download the full arduino source code here.

Final adjustment

But wait you might want to re-calibrate the sensor alarm to suit your need. This is not just for show, it should trigger and alarm when the threshold of gas reach a certain level. Depends on the type of sensor that you are using you will need to calibrate this. So go get a lighter, and point the lighter towards the sensor, and without lighting the lighter, press the gas release button on the lighter, so the gas will flow to the sensor. This should trip the buzzer. If not then you need to check whether the reading goes up by looking at the webserver. If this is not working then you need to check the connection, the sensor and the buzzer. If all goes well, the buzzer should make a noise. The threshold in the code is set to 100, you should be able to find it in the following section of the code:

double threshold = 100;

Feel free to change the threshold to higher or lower depends on your need. I hope you like this project. If you do please drop me a line, and subscribe to my blog for more projects.

 

Posted on Leave a comment

Arduino Dice simulator

I was looking at my kids playing snake and ladder tonight, and came an idea in my mind “why don’t I make a dice simulator?”. I already had the LED matrix from the previous post here. So I created the Arduino Dice simulator using my previous circuit.

So I added a permanent button to the circuit that I had using a pull-down resistor of 10K ohm which connects pin 2 to the ground.

Then with little modification to the code as following

// LED matrix with latch 74HC595N
// the program random display the character
// the button is added and the character will only be displayed if after the button is pressed
// added code to simulate a dice

int latchPin = 4; // pis connected to shift registors
int clockPin = 5;
int dataPin = 3;
int pins [8] = {6, 7, 8, 9, 10, 11, 12, 13}; // common cathode pins
int button = 2; // connect button and 10K resistor to GND (normal LOW), the other end to 5V
byte A[8] = {   B00000000, // Letters are defined
                B00011000,// you can create your own
                B00100100,
                B01000010,
                B01111110,
                B01000010,
                B01000010,
                B00000000
            };
byte B[8] = {0x0, 0x7c, 0x42, 0x42, 0x7c, 0x42, 0x42, 0x7c}; 
byte C[8] = {0x0, 0x3c, 0x42, 0x40, 0x40, 0x40, 0x42, 0x3c};
byte D[8] = {0x0, 0x7c, 0x42, 0x42, 0x42, 0x42, 0x42, 0x7c};
byte E[8] = {0x0, 0x7e, 0x40, 0x40, 0x7c, 0x40, 0x40, 0x7e};
byte F[8]={0x0, 0x7e, 0x40, 0x40, 0x7c, 0x40, 0x40, 0x40};
byte G[8]={0x0, 0x3c, 0x42, 0x40, 0x4e, 0x42, 0x42, 0x3c};
byte H[8]={0x0, 0x42, 0x42, 0x42, 0x7e, 0x42, 0x42, 0x42};
byte I[8]={0x0, 0x70, 0x20, 0x20, 0x20, 0x20, 0x20, 0x70};
byte J[8]={0x0, 0x7c, 0x4, 0x4, 0x4, 0x4, 0x44, 0x38};
byte K[8]={0x0, 0x44, 0x48, 0x50, 0x60, 0x50, 0x48, 0x44};
byte L[8]={0x0, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x7c};
byte M[8]={0x0, 0x42, 0x66, 0x5a, 0x42, 0x42, 0x42, 0x42};
byte N[8]={0x0, 0x42, 0x62, 0x72, 0x5a, 0x4e, 0x46, 0x42};
byte O[8]={0x0, 0x3c, 0x42, 0x42, 0x42, 0x42, 0x42, 0x3c};
byte P[8]={0x0, 0x7c, 0x42, 0x42, 0x7c, 0x40, 0x40, 0x40};
byte Q[8]={0x0, 0x3c, 0x42, 0x42, 0x42, 0x4a, 0x44, 0x3a};
byte R[8]={0x0, 0x7c, 0x42, 0x42, 0x7c, 0x48, 0x44, 0x42};
byte S[8]={0x0, 0x3e, 0x40, 0x40, 0x3c, 0x2, 0x2, 0x7c};
byte T[8]={0x0, 0x7c, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10};
byte U[8]={0x0, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x3c};
byte V[8]={0x0, 0x42, 0x42, 0x42, 0x42, 0x42, 0x24, 0x18};
byte W[8]={0x0, 0x42, 0x42, 0x42, 0x42, 0x5a, 0x66, 0x42};
byte X[8]={0x0, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x0};
byte Y[8]={0x0, 0x44, 0x44, 0x28, 0x10, 0x10, 0x10, 0x10};
byte Z[8]={0x0, 0x7e, 0x4, 0x8, 0x10, 0x20, 0x40, 0x7e};
byte X1[8]={0x0, 0x0, 0x18, 0x3c, 0x3c, 0x18, 0x0, 0x0};
byte X2[8]={0x0, 0x20, 0x70, 0x20, 0x4, 0xe, 0x4, 0x0};
byte X3[8]={0x40, 0xe0, 0x40, 0x18, 0x18, 0x2, 0x7, 0x2};
byte X4[8]={0x42, 0xe7, 0x42, 0x0, 0x0, 0x42, 0xe7, 0x42};
byte X5[8]={0x42, 0xe7, 0x42, 0x18, 0x18, 0x42, 0xe7, 0x42};
byte X6[8]={0x66, 0x66, 0x0, 0x66, 0x66, 0x0, 0x66, 0x66};
byte blank[8] = { B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000};
long randNumber;
int buttonpressed = 0;
long time = 0; // last time the button pressed
long debounce = 50;

void setup() {
  Serial.begin(9600); // Serial begin
  pinMode(latchPin, OUTPUT); // Pin configuration
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  for (int i = 0; i < 8; i++) { // for loop is used to configure common cathodes
    pinMode(pins[i], OUTPUT);
    digitalWrite(pins[i], HIGH);
  }
  pinMode(button, INPUT);   // set button input 
  randomSeed(analogRead(0));
}

void loop() {
  // read the button
  buttonpressed = digitalRead(button);
  if (buttonpressed == HIGH && millis() - time > debounce){
    time = millis();
    randNumber = random(1,6);
    Serial.println(randNumber);
    
    for (int k = 0; k < 1000; k++) { // showing each letter for 1 second
      getChar(randNumber);
  
    }       
  } else {
    display_char(X1);delay(20);
    display_char(X2);delay(20);
    display_char(X3);delay(20);  
    display_char(X4);delay(20);
    display_char(X5);delay(20);
    display_char(X6);delay(20);
 
  }


  // add more letters show method here

}

void getChar(int num){
  switch (num){
    case 1: display_char(X1); break;
    case 2: display_char(X2); break;
    case 3: display_char(X3); break;
    case 4: display_char(X4); break;
    case 5: display_char(X5);break;
    case 6: display_char(X6);break;
    default:
    break;
  }

}
void display_char(byte ch[8]) { // Method do the multiplexing
  for (int j = 0; j < 8; j++) {
    digitalWrite(latchPin, LOW);
    digitalWrite(pins[j], LOW);

    shiftOut(dataPin, clockPin, LSBFIRST, ch[j]);
    digitalWrite(latchPin, HIGH);
    //delay(1);
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, B00000000); // to get rid of flicker when
    digitalWrite(latchPin, HIGH);
    digitalWrite(pins[j], HIGH);

  }
}


I hope you enjoyed this short post. You can view the result in the following video

If you like what I post, don’t forget to subscribe.

Posted on Leave a comment

RFID enabled tag

This RFID enabled tag tutorial show a simple RFID circuit that connects to Arduino. The circuit will detect the RFID card and play a simple tone on the speaker when the registered card is tapped and will play another tone when un-registered card is tapped.

RFID with speaker1

I had stumble across a simple circuit to simulate RFID lock and unlock via Instructables from educ8s and his website. So I used his code with little modification and added my own code from the previous post on simple Super mario speaker song, and this circuit was born.

Continue reading RFID enabled tag