Show Posts
Pages: [1] 2 3 4
1  Using Arduino / Programming Questions / Re: Setting Arduino's date and time? on: March 18, 2013, 06:02:03 am
I've come back here as it seems logical to continue this discussion rather than starting a new thread as I think my current problem is related.

As you can see below I got everything to work as I wanted with my Ethernet Shield and then with my Ethernet Arduino.

Now I'm trying to apply the same principles to a much simpler project: a WiFi smoke detector and thermometer.

Where I am blocking is setting the time for the Arduino. It appears as though my original approach, which worked via Ethernet and Ethernet UDP, isn't going to work with WiFi. I am hoping that someone here is going to tell me that the following thread is dated and this (somehow) works now:

Before anyone asks, and in case it's just a matter of programming, here is an example of the code I am working on:
  WiFi Time Server Test

// Libraries
#include <SPI.h>
#include <WiFi.h>
#include <EthernetUdp.h>
#include <Time.h>

// NTP time stamp from first 48 bytes of the message
const int iNTP_PACKET_SIZE = 48;

// Buffer to hold incoming / outgoing packets
byte byPacketBuffer[iNTP_PACKET_SIZE];

// Previous hour and minute counters
byte byPrevHr = 0;
byte byPrevMin = 0;

// UDP instance
EthernetUDP Udp;

// GetNTPTime
// Get the time via NTP, adjust for Time Zone and return for synchronisation
unsigned long GetNTPTime() {
  // Time Zone (Difference from GMT in seconds)
  const long lTZ = 3600;

  // Time Server fixed IP address (
  IPAddress ipTimeServer(64, 90, 182, 55);

  // Set an NTP packet to the NTP time server

  if (Udp.parsePacket()) { 
    // We've received a packet, read the data from it, iNTP_PACKET_SIZE);

    //the timestamp starts at byte 40 of the received packet and is four bytes,
    // or two words, long. First, extract the two words:
    unsigned long highWord = word(byPacketBuffer[40], byPacketBuffer[41]);
    unsigned long lowWord = word(byPacketBuffer[42], byPacketBuffer[43]); 
    // combine the four bytes (two words) into a long integer
    // this is NTP time (seconds since Jan 1 1900):
    unsigned long secsSince1900 = highWord << 16 | lowWord; 

    // now convert NTP time into everyday time:
    // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
    const unsigned long seventyYears = 2208988800UL;
    // subtract seventy years:
    unsigned long epoch = secsSince1900 - seventyYears;
    // Adjust for local time zone
    epoch += lTZ;
    // Return local time
// PrintTime
// Serial Print time (DD/MM/YYYY - HH:MM:SS)
void PrintTime() {
  char cTime[22];
    sprintf(cTime, "%02u/%02u/%4u - %02u:%02u:%02u", day(), month(), year(), hour(), minute(), second());

// sendNTPpacket
// Send an NTP request to the NTP time server at the IP address
unsigned long sendNTPpacket(IPAddress& address) {

  // set all bytes in the buffer to 0
  memset(byPacketBuffer, 0, iNTP_PACKET_SIZE);

  // Initialize values needed to form NTP request
  // (see URL above for details on the packets)
  byPacketBuffer[0] = 0b11100011;   // LI, Version, Mode
  byPacketBuffer[1] = 0;     // Stratum, or type of clock
  byPacketBuffer[2] = 6;     // Polling Interval
  byPacketBuffer[3] = 0xEC;  // Peer Clock Precision
  // 8 bytes of zero for Root Delay & Root Dispersion
  byPacketBuffer[12]  = 49;
  byPacketBuffer[13]  = 0x4E;
  byPacketBuffer[14]  = 49;
  byPacketBuffer[15]  = 52;

  // all NTP fields have been given values, now
  // you can send a packet requesting a timestamp:         
  Udp.beginPacket(address, 123);   //NTP requests are to port 123
  Udp.write(byPacketBuffer, iNTP_PACKET_SIZE);

// WiFiConnect
// Establish WiFi connection to network
byte WiFiConnect() {
  byte byResult = false;
  // WiFi Network & Password
  char SSID[] = "SSIDHERE";
  char WiFiPass[] = "PASSWORDHERE";
  int iWiFiStatus = WL_IDLE_STATUS;     // WiFi radio's status
  byte byAttempt = 0;                   // Attempt counter

  while (iWiFiStatus != WL_CONNECTED) {
    if (byAttempt < 10) {

      // Wait 3 seconds before trying

      Serial.print(F("Attempting to connect to WPA network: "));
      Serial.print(F("\nAttempt: "));

       // Attempt to connect to WPA network:
      iWiFiStatus = WiFi.begin(SSID, WiFiPass);
      if (iWiFiStatus != WL_CONNECTED) {
        byResult = false;

        Serial.println(F("Couldn't establish WiFi connection"));
      // if connected :
      else {
        byResult = true;
        Serial.println(F("WiFi connection established\n"));
    else {
      byResult = 2;    // Too many attempts

void setup() {
  // Setup serial monitor
  // Wait 3 seconds
  Serial.println(F("WiFi Time Test"));
  Serial.println(F("Arduino - Derek Erb\n"));
  // Connect to WiFi

  // Setup time
  Serial.println(F("Waiting for sync...\n"));

  while(timeStatus() == timeNotSet);  // Wait until time set by sync provider

  // Display time
  // Save current hour and minute as previous hour and minute
  byPrevHr = hour();
  byPrevMin = minute();
void loop() {
  // Display new time at the start of every minute
  if (byPrevMin != minute()) {
    // Store current minute as new previous minute
    byPrevMin = minute();
    // Serial print time

If not... then how does one go about setting the time (autonomously) in an Arduino with a WiFi shield???

2  Using Arduino / General Electronics / Re: How to estimate number of hours remaining before battery dies? on: March 13, 2013, 06:54:01 am
The secret voltmeter appears to work quite well on my Arduino Uno.

When I get the time I'll play around with dimming a LED as the "voltmeter" reduces...

3  Using Arduino / General Electronics / Re: How to estimate number of hours remaining before battery dies? on: March 13, 2013, 05:58:16 am
I too want to be able to display something on my project before the battery completely dies off...

How does one "measure the voltage" programmatically within the sketch?

Is there some way to do this without adding a transistor?

Great ideas here... thanks!
4  Using Arduino / General Electronics / Re: Replacing a pushbutton with a on/off rocker switch on: March 12, 2013, 06:13:15 pm
Even though it works I still have a question...

I just copied the resistor which had been used in the pushbutton tutorial. This 1K resistor seems to work fine at the moment.

But, normally, how would I have calculated what value resistor would have been right for this rocker switch?

5  Using Arduino / General Electronics / Re: Replacing a pushbutton with a on/off rocker switch on: March 12, 2013, 05:55:29 pm
I wired up the rocker switch exactly as I had wired up the pushbutton and it worked!

One post goes to 5V. One post goes to one end of 1K resistor and pin 7 the other end of the 1K resistor goes to Gnd.

My code required very little modification, as I still get a 1 or a 0 from the switch, and it works even better than the push button did - for this project.

6  Using Arduino / General Electronics / Re: Replacing a pushbutton with a on/off rocker switch on: March 12, 2013, 04:50:46 pm
Thank you for your reply and explanation.

Do I use the same resistor? Is the 1K enough or too much?

Where do the 2 ends of the resistor go? One end goes to the wire going to Gnd right? Does the other end of the resistor go to the wire going to the input pin?

I thought the push button, and therefore switch, had to be connected to power (5v) and the input pin as in the tutorial I learned from :
7  Using Arduino / General Electronics / Replacing a pushbutton with a on/off rocker switch on: March 12, 2013, 02:58:09 pm
On my project I currently have a tiny pushbutton which my code waits for someone to press on. I then toggle the project on/off, in the loop() function, each time someone presses the button.

Currently my tiny pushbutton is connected using 2 of the 4 posts coming out of it: 1 post is connected to the Arduino Uno's 5V out and the other post is connected to pin 7 with a 1K resistor going to the GND pin.

My tiny pushbutton is this one:

I realised that what I really wanted was a simple On/Off switch. So I went out and got this one:

My question therefore is how I plug this thing in to my project. It's the electronics (hardware) side where I am still flying blind or with really dark and foggy sunglasses.

I can see that this rocker switch is rated up to 16A at 125V from the sales sheet. I see numbers like that on the side of the switch as well as things like 1/3HP and 125/250VAC. I believe I know what the A (amps), V (volts) and VAC (volts alternating current?) stand for but I have no idea what the HP stands for. I assume horsepower doesn't fit in to this equation {vbg}.

I looked at the Mechanical Drawing ( naively thinking this might help me figure out how to wire this up. I know just about every word on that sheet of paper is in English. But it's like showing the Kama Sutra to a virgin. I just don't know what fits where...

Am I so lucky that I can just replace the 2 posts on my button with the 2 posts on this rocker switch (one going to 5V and the other going to pin 7 and GND with a 1K resistor)? Does it matter which post is which?

Or should I be looking at this switch totally differently than I would a pushbutton?

Thank you in advance both for any practical help you could provide and, if you have the time, any explanation you could add to let me know how I could have, and perhaps should have, figured this out for myself.
8  Using Arduino / Sensors / Re: Hooking up a gas & smoke (MQ-2) sensor on: February 26, 2013, 11:21:10 am
That last message from Erdin was what I needed. The resistor needs to be connected to both the GND and the Analog input. You can see the newly corrected Fritzing attached.

I knew it had worked as soon as I ran the original code on the new wiring. It started with values around 790-810 and slowly went down and down to the 500s and then the 400s and finally it settled around the upper 360s after 15 minutes. That's when I decided to test. I lit a match underneath the sensor and it went crazy. It shot to 800 immediately. I blew out the match and it stayed up in the 800 range and hovered there while the smoke billowed in my hand. When I moved my hand away and let the smoke dissipate it dropped to 600 and, a few seconds later, slowly worked its way back down to the upper 360s.

I modified the code slightly to map the values as a percentage: in other words I took the incoming range of 0 to 1023 and applied it to a range of 0 to 100. The "standard" mapped reading seems to be around 35-36 now and I can therefore check for differences from that to see if it's in the presence of smoke.

  MQ-2 Sensor connected to Arduino Uno
  Both A pins and left H pin connected to 5V out
  Right H pin connected to GND
  Both B pins, and right H pin, connected to 22k ohms resistor and A0

// Sensor value read from analog pin (range from 0 to 1023)
int iSensorValue = 0;

// Mapped value from 0 to 100
byte bySensorVal = 0;

// Message string to be displayed on serial monitor
char cMsg[124];

void setup() {

void loop() {
  // Read input value on A0 and map it from 0 to 100
  iSensorValue = analogRead(A0);
  bySensorVal = map(iSensorValue, 0, 1023, 0, 100);
  // Display input value and mapped value
  sprintf(cMsg, "MQ-2 Sensor Value : %d (%d)", iSensorValue, bySensorVal);

  // Check for high value
  if (bySensorVal > 60) {
    Serial.println(F(" *** DISTURBANCE IN THE FORCE! ***"));
  else {
  // Loop 10 times per second

Now I need to find some different gas samples to play around with to see how the sensor reacts ... but somehow without making myself sick at the same time.

Thank you ALL for your helpful and detailed replies. I am just at the beginnings of what I want this little device to do, and this is one of the simplest of the projects I have in my head, and I have already learned a TON here.
9  Using Arduino / Sensors / Re: Hooking up a gas & smoke (MQ-2) sensor on: February 25, 2013, 06:17:20 pm
So, as I have 4 other MQ-2 sensors and I can be quite impatient and it's late. I plugged in the setup as described.

Nothing blew up so I guess that's a good sign.

I tried using the following very simplistic code
  MQ-2 Sensor connected to Arduino Uno
  Both A pins and left H pin connected to 5V out
  Right H pin connected to GND
  Both B pins conncted to 22k ohms resistor and A0

int iSensorValue = 0;

void setup() {

void loop() {
  // Read value on A0
  iSensorValue = analogRead(A0);
  // Display value
  Serial.print(F("MQ-2 Sensor Value = "));
  // Loop 10 times per second

I get a value ranging from 1019 to 1022 every 1/10 of a second.

Unfortunately even when I light a match directly under the sensor, blow it out, cup my hand over it and engulf the sensor in smoke those values never change to anything lower or different.

I'm thinking that they should be lower at the start, "at ease" as it were, and go higher when the sensor comes in contact with smoke. But, as I said, the standard values it shows, upon startup, continually range from 1019 to 1022 which seems to me to be basically at the maximum ceiling of 1023...

I'm hoping that means I have just screwed up something in the wiring somewhere... but not enough to fry the sensor?
10  Using Arduino / Sensors / Re: Hooking up a gas & smoke (MQ-2) sensor on: February 25, 2013, 06:08:48 pm
I'm sorry... the Fritzing files I uploaded were wrong. They didn't correspond to the wiring I had done.

That incorrect version had a black wire going from GND on the breadboard to 5V on the Arduino and nothing going from the power row to the Arduino.

This new version has the correct, in that it corresponds to what I've done, wiring.
11  Using Arduino / Sensors / Re: Hooking up a gas & smoke (MQ-2) sensor on: February 25, 2013, 04:43:48 pm
Erdin : Thank you so much for your very helpful and detailed reply. I believe I was able to both understand and follow your instructions.

I tried taking pictures but you can't see much on them. I made a Fritzing diagram instead which you'll find attached.

I have attached red wires to both A bins and the left H pin.
I have attached a white wire to the right H pin.
I have attached green wires to both B pins.

On the breadboard you'll notice a 22k ohms resistor in the same column as the 2 green wires and heading to the wire which goes to A0.

I truly appreciate your having a look before I power this up.

Lefty (who I see all over these forums!) : I should have been clearer in my original point. I bought several MQ-2 sensors. But I am only using 1. When one is learning electronics and tinkering by trial and error one must allow for "replacement parts"... just in case.

[Incorrect Fritzing attached files removed]
12  Using Arduino / General Electronics / Re: Very basic resistors question on: February 25, 2013, 01:41:11 pm
Thank you again for your detailed and helpful replies.

For calculation purposes you can just assume a digital output pin set to HIGH will have the same voltage as the Vcc value powering the chip, so +5vdc for a 5 volt board and +3.3 volts for a 3.3 volt board.

Is an Arduino Uno a 5 volt board or a 3.3 volt board?

Would all of this work the same if I go through an analog port instead of a digital port?
13  Using Arduino / Sensors / Hooking up a gas & smoke (MQ-2) sensor on: February 25, 2013, 01:36:59 pm
I am trying to build a networked smoke & gas detector. I got a few MQ-2 sensors to hook up to my Arduino (Uno with WiFi Shield) via my breadboard.

My local suppliers didn't have the ones with the breakout boards so I have the "raw" 6-pin sensors.

I have gone through the data sheet ( as best I can. I have also looked at the Arduino Playground page ( as regards the sensors and tried to follow that as best as I can. I have also looked at some video examples. But they all seem to be using breakout boards for their connections...

But I am lost in the wiring. Basically I see there are 2x A pins, 2x B pins and 2x H pins. My understanding is that I should be connecting the two A pins together, the two B pins together and the 2 H pins together. My assumption therefore was to run two wires from the two A pins to two holes in the breadboard but in the same column, two wires from the two B pins to a same column in the breadboard and two wires from the two H pins to a same column in the breadboard. I would then run one wire from the A pin column in the breadboard to the Arduino, one from the B pin column to the Arduino and one from the H pin column to the Arduino (ie 3 wires from the breadboard to the Arduino).

The sensor appears to take 5V directly and I am going to need to add a load resistor (from 2kOhm to 47kOhm) connected to the ground part of the circuit.

My basic problem is that I can't decipher which of the sensor pins goes where. I believe one of the pins goes to the 5V, one goes to GND and one goes to an analog input port such as A0. But I don't know which pin (pair - column) corresponds to each of these.

From the Playground page I believe the A pins go to the 5V. But I'm not sure. I then don't know which goes to the GND (B pins or H pins). Whichever goes to GND then gets the resistor between it and the GND on the Arduino and the other pin pair goes to A0.

Any ideas or suggestions?

Many thanks.
14  Using Arduino / General Electronics / Re: Very basic resistors question on: February 25, 2013, 12:41:51 pm
I now understand that the resistor can be connected on the cathode pin or the anode pin.

If I am running a wire from the digital, or analog, output pin on the Arduino to the cathode wire of the LED I assume it is the digital, or analog, output pin on the Arduino which is sending the current to the LED. If I am using one of these output pins, rather than the 5V or 3.3V pins, how do I know how many volts are going to the circuit and therefore which resistor calculation to use?
15  Using Arduino / General Electronics / Re: Very basic resistors question on: February 25, 2013, 12:29:52 pm
I understand that Uf should be Vf. But on the LEDs I get they all say Uf

The important thing is that I now understand that these values are forward voltage and what they are used for.

For an output pin of 5V with a LED of Vf 2V and an lf of 20mA I would use a resistor of 150 ohms
(5 -2) / 0.02 = 150

For an output pin of 3.3V with a LED of Vf 3V and an lf of 20mA I would use a resistor of 15 ohms
(3.3 - 3) / 0.02 = 15

As regards the circuit, if I've understood correctly and following the examples I've seen in the books, I run the power from the output pin on the Arduino (3.3V or 5V) to the anode wire of the LED, a wire from the cathode wire of the LED to the resistor (using the calculation above) and a wire from the other end of the reistor to the digital or analog output pin of the Arduino.

I think I am, very slowly but surely, getting there.

I've been following the examples in the books, and blindly plugging in the right components in the right holes, without really understanding why.

Thank you all so much for your explanations.
Pages: [1] 2 3 4