Ruben Laguna’s blog

Arduino Tilt Sensor Using MMA7260Q Accelerometer

I’ve playing with the MMA7260Q accelerometer. Following the indications in

I connected X,Y and Z of the MMA7260Q breakout to A0, A1 and A2 of the Arduino. Then, GS1 and GS2 connected to ground to set the range to 1.5g. Ah, and of course, I connected ^SLEEP to VCC.

I managed to put together this piece of code that ouputs the tilt angles ρ(rho) , φ(phi); and Θ(theta) in degrees. I does automatic autozero calibration so before getting sensible results you should tilt the sensor around +/-90 degrees in every axis.

Dropbox Not Syncing Files

Today I fixed my dropbox installation on mac os x . It was not updating (sending) files from the Mac to the Dropbox servers. In fact it was sending new files to dropbox servers but some old files that were supposed to be on dropbox account were not synchonized and I couldn’t get Dropbox to notice that, until today. I found this post and it seems that if you close dropbox, delete the ~/.dropbox directory and restart dropbox you’ll get the account linking screen again and dropbox will index everything and make sure that your local Dropbox directory match the online one. It took a while but all the files are safely transferred to Dropbox now.

Arduino Sleep Mode - Waking Up When Receiving Data on the USART

I’ve been playing with the Arduino sleep modes and i wanted to be able to wake up from the sleep when receiving data on the serial port. Mainly, because in my project I’m using the XBee in the API mode and the tricks exposed in http://www.arduino.cc/playground/Learning/ArduinoSleepCode and http://www.libelium.com/squidbee/index.php?title=Save_power_in_SquidBee_-sleep_mode involve putting Arduino in SLEEP_MODE_PWRDOWN and using an extra pin on the arduino to monitor the RX pin and detecting LOW.

I didn’t like that much that solution so I started to look into other ways of doing it without using an extra pin and without risk of losing data in the serial interface. Because as I understood it using SLEEP_MODE_PWR_DOWN requires to send first a burst of data to the serial interface in order to wake up the arduino. And it takes a while for the Arduino to become fully functional so that means that you will lose/miss data in the serial interface. That was something that didn’t fit my project.

In order to be able to sleep but without missing serial data I used POWER_MODE_IDLE, a power saving mode that leaves the USART on and then using the functions defined in power.h (you have to use arduino-12 to get power.h) I disabled all other modules that I don’t need to cut down the power consumption. When any data is received in the USART the Arduino will be brought back to normal power mode (USART uses interrupts and any interrupt makes the ATmega168 to exit the power saving mode).

See the actual code below (or in gist)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<span class='line'>/* Sleep Demo Serial
</span><span class='line'> * -----------------
</span><span class='line'> * Example code to demonstrate the sleep functions in a Arduino. Arduino will wake up
</span><span class='line'> * when new data is received in the serial port USART
</span><span class='line'> * Based on Sleep Demo Serial from http://www.arduino.cc/playground/Learning/ArduinoSleepCode 
</span><span class='line'> *
</span><span class='line'> * Copyright (C) 2006 MacSimski 2006-12-30 
</span><span class='line'> * Copyright (C) 2007 D. Cuartielles 2007-07-08 - Mexico DF
</span><span class='line'> * 
</span><span class='line'> *  With modifications from Ruben Laguna  2008-10-15
</span><span class='line'> * 
</span><span class='line'> *  This program is free software: you can redistribute it and/or modify
</span><span class='line'> *  it under the terms of the GNU General Public License as published by
</span><span class='line'> *  the Free Software Foundation, either version 3 of the License, or
</span><span class='line'> *  (at your option) any later version.
</span><span class='line'> * 
</span><span class='line'> *  This program is distributed in the hope that it will be useful,
</span><span class='line'> *  but WITHOUT ANY WARRANTY; without even the implied warranty of
</span><span class='line'> *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
</span><span class='line'> *  GNU General Public License for more details.
</span><span class='line'> * 
</span><span class='line'> *  You should have received a copy of the GNU General Public License
</span><span class='line'> *  along with this program.  If not, see &lt;http://www.gnu.org/licenses/>.
</span><span class='line'> * 
</span><span class='line'> */
</span><span class='line'>
</span><span class='line'>#include &lt;avr/power.h>
</span><span class='line'>#include &lt;avr/sleep.h>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'>int sleepStatus = 0;             // variable to store a request for sleep
</span><span class='line'>int count = 0;                   // counter
</span><span class='line'>
</span><span class='line'>
</span><span class='line'>void setup()
</span><span class='line'>{
</span><span class='line'>
</span><span class='line'>  Serial.begin(9600);
</span><span class='line'>}
</span><span class='line'>
</span><span class='line'>void sleepNow()
</span><span class='line'>{
</span><span class='line'>    /* Now is the time to set the sleep mode. In the Atmega8 datasheet
</span><span class='line'>     * http://www.atmel.com/dyn/resources/prod_documents/doc2486.pdf on page 35
</span><span class='line'>     * there is a list of sleep modes which explains which clocks and 
</span><span class='line'>     * wake up sources are available in which sleep modus.
</span><span class='line'>     *
</span><span class='line'>     * In the avr/sleep.h file, the call names of these sleep modus are to be found:
</span><span class='line'>     *
</span><span class='line'>     * The 5 different modes are:
</span><span class='line'>     *     SLEEP_MODE_IDLE         -the least power savings 
</span><span class='line'>     *     SLEEP_MODE_ADC
</span><span class='line'>     *     SLEEP_MODE_PWR_SAVE
</span><span class='line'>     *     SLEEP_MODE_STANDBY
</span><span class='line'>     *     SLEEP_MODE_PWR_DOWN     -the most power savings
</span><span class='line'>     *
</span><span class='line'>     *  the power reduction management &lt;avr/power.h>  is described in 
</span><span class='line'>     *  http://www.nongnu.org/avr-libc/user-manual/group__avr__power.html
</span><span class='line'>     */  
</span><span class='line'>     
</span><span class='line'>  set_sleep_mode(SLEEP_MODE_IDLE);   // sleep mode is set here
</span><span class='line'>
</span><span class='line'>  sleep_enable();          // enables the sleep bit in the mcucr register
</span><span class='line'>                             // so sleep is possible. just a safety pin 
</span><span class='line'>  
</span><span class='line'>  power_adc_disable();
</span><span class='line'>  power_spi_disable();
</span><span class='line'>  power_timer0_disable();
</span><span class='line'>  power_timer1_disable();
</span><span class='line'>  power_timer2_disable();
</span><span class='line'>  power_twi_disable();
</span><span class='line'>  
</span><span class='line'>  
</span><span class='line'>  sleep_mode();            // here the device is actually put to sleep!!
</span><span class='line'> 
</span><span class='line'>                             // THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP
</span><span class='line'>  sleep_disable();         // first thing after waking from sleep:
</span><span class='line'>                            // disable sleep...
</span><span class='line'>
</span><span class='line'>  power_all_enable();
</span><span class='line'>   
</span><span class='line'>}
</span><span class='line'>
</span><span class='line'>
</span><span class='line'>void loop()
</span><span class='line'>{
</span><span class='line'>  // display information about the counter
</span><span class='line'>  Serial.print("Awake for ");
</span><span class='line'>  Serial.print(count);
</span><span class='line'>  Serial.println("sec");
</span><span class='line'>  count++;
</span><span class='line'>  delay(1000);                           // waits for a second
</span><span class='line'>
</span><span class='line'>  // compute the serial input
</span><span class='line'>  if (Serial.available()) {
</span><span class='line'>    int val = Serial.read();
</span><span class='line'>    if (val == 'S') {
</span><span class='line'>      Serial.println("Serial: Entering Sleep mode");
</span><span class='line'>      delay(100);     // this delay is needed, the sleep 
</span><span class='line'>                      //function will provoke a Serial error otherwise!!
</span><span class='line'>      count = 0;
</span><span class='line'>      sleepNow();     // sleep function called here
</span><span class='line'>    }
</span><span class='line'>    if (val == 'A') {
</span><span class='line'>      Serial.println("Hola Caracola"); // classic dummy message
</span><span class='line'>    }
</span><span class='line'>  }
</span><span class='line'>
</span><span class='line'>  // check if it should go asleep because of time
</span><span class='line'>  if (count >= 10) {
</span><span class='line'>      Serial.println("Timer: Entering Sleep mode");
</span><span class='line'>      delay(100);     // this delay is needed, the sleep 
</span><span class='line'>                      //function will provoke a Serial error otherwise!!
</span><span class='line'>      count = 0;
</span><span class='line'>      sleepNow();     // sleep function called here
</span><span class='line'>  }
</span><span class='line'>}</span>

Upgrading Xbee Series 2 (XbeeShield) to Xbee ZB With Arduino

As stated in this digi page is possible to upgrade/convert a Xbee Series 2.5 to a Xbee ZB (with full standard Zigbee capabilities). Xbee Series 2.5 is just another name for the forme Series 2, so the upgrade procedure is exactly the same.

The procedure if fairly straighforward

  1. Remove ATmega168 from Arduino board
  2. Insert XbeeShield into Arduino
  3. Change *both *jumpers in XbeeShield to USB
  4. Connect Arduino to the computer using the USB cabke
  5. Download the ZNet 2.5 to ZB Conversion Kit here
  6. Start the X-CTU 5.1.4.1 software
  7. xbee01
  8. Select the proper COM port (should be Usb serial port (COMX))
  9. Click on Test Query. The following dialog should appear indicating that X-CTU found the Xbee in that COM port and giving you some information about the Xbee module version, etc. In my case it was a ZB24-B with 1220 firmware
  10. xbee02

  11. Go to the Modem Configuration tab
  12. Download the ZNet 2.5 to ZB Conversion Kit here if you didn’t do it earlier
  13. Uncompress the ZIP
  14. xbee11

  15. Select the “Download new versions” button.

  16. xbee12
  17. Press the “File” source button

  18. Select the xbee_zb.zip file to add the firmware set to X-CTU
  19. Back to the Modem Configuration tab. Select “Always update firmware” checkbox and select XB24-ZB (or XB24P-ZB if you have a PRO module) and the function set that you want for the module (router, coordinator, end device, each one in two differente flavor AT or API). In my case I selected XB24-ZB and COORDINATOR API. Check the product manual to find out about API and AT modes and the different firmwares for each set
  20. xbee03

  21. Press “Write” and X-CTU will start uploading the firmware to the module
  22. xbee04
    xbee05

  23. The firmware upgrade is complete

NOTE: Make sure that the configuration for DIO7 is DISABLED as this XBee pin is connected to the RST in the Arduino. If you leave the default option (1 – CTS) random resets after a couple of seconds may occur+

NOTE 2: Be aware that putting XBee to sleep will also raise DIO7 and produce and Arduino reset. See this other post on XBee sleep and Arduino reset

Arduino and DS1620 Digital Temperature Sensor

I received a couple of Arduinos Diecimilla that I bought from Libelium.

My first project with Arduino has been interfacing with DS1620 digital temperature sensor.

Picture 17

It was really easy it just a matter of connecting pin 3, 4 and 5 on the Arduino to RST, CLK and DQ on the DS1620. The source code in this post will read the temperature from the DS1620 using the 3 wire interface and it will output the result to the serial interface in the Arduino.

IMG_0329

I mostly use the information of PC Parallel Port Interfacing with DS1620 Digital Thermometer / Thermostat page to learn how to interface to this thermometer and the information on the SerialPortExample page to learn how to output to the serial port.

The DS1620 measures temperatures from -55°C to +125°C
in 0.5°C increments. As you can see in the code reading a DS1620 sensor is way more complicated than reading an analog LM35 temperature sensor (and the LM35 gives you more precision and also takes one pin instead of three) so probably is a LM35 is a better option if you want an easy way to get the temperature in your arduino project.

Source code (gist):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<span class='line'>/*
</span><span class='line'>  DS160 example
</span><span class='line'>
</span><span class='line'>  Reading temperature from DS1620 digital temperature sensor 
</span><span class='line'>  and showing the result via serial interface.
</span><span class='line'>  
</span><span class='line'>  
</span><span class='line'>  Arduino    DS1620
</span><span class='line'>  pin 3  ->    RST
</span><span class='line'>  pin 4  ->    CLK
</span><span class='line'>  pin 5  ->    DQ
</span><span class='line'>  
</span><span class='line'>  by Ruben Laguna
</span><span class='line'>  based on examples from Tom Tigoe &lt;http://www.arduino.cc/en/Reference/SoftwareSerialExample>
</span><span class='line'>  and phanderson &lt;http://www.phanderson.com/printer/ds1620/ds1620.html>
</span><span class='line'>  written: 30 Aug 2008
</span><span class='line'>  
</span><span class='line'>
</span><span class='line'>*/
</span><span class='line'>
</span><span class='line'>// include the SoftwareSerial library so you can use its functions:
</span><span class='line'>#include &lt;SoftwareSerial.h>
</span><span class='line'>
</span><span class='line'>#define rxPin 0
</span><span class='line'>#define txPin 1
</span><span class='line'>#define ledPin 13
</span><span class='line'>#define buttonPin 2
</span><span class='line'>
</span><span class='line'>#define rstPin 3
</span><span class='line'>#define clkPin 4
</span><span class='line'>#define dqPin 5
</span><span class='line'>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'>// set up a new serial port
</span><span class='line'>SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);
</span><span class='line'>byte pinState = 0;
</span><span class='line'>
</span><span class='line'>void setup()  {
</span><span class='line'>  // define pin modes for tx, rx, led pins:
</span><span class='line'>  pinMode(rxPin, INPUT);
</span><span class='line'>  pinMode(txPin, OUTPUT);
</span><span class='line'>  pinMode(buttonPin, INPUT);
</span><span class='line'>  pinMode(ledPin, OUTPUT);
</span><span class='line'>  pinMode(rstPin, OUTPUT);
</span><span class='line'>  pinMode(clkPin, OUTPUT);
</span><span class='line'>  pinMode(dqPin, OUTPUT);
</span><span class='line'>  
</span><span class='line'>  // set the data rate for the SoftwareSerial port
</span><span class='line'>  mySerial.begin(9600);
</span><span class='line'>}
</span><span class='line'>
</span><span class='line'>
</span><span class='line'>void loop() {
</span><span class='line'>  int val = digitalRead(buttonPin);
</span><span class='line'>        
</span><span class='line'>    rst_low();
</span><span class='line'>    
</span><span class='line'>    clk_high();
</span><span class='line'>    rst_high(); //all data transfer are initiated by driving RST high
</span><span class='line'>    write_command(0x0c); // write config command
</span><span class='line'>    write_command(0x02); // cpu mode
</span><span class='line'>    rst_low();
</span><span class='line'>    delay(200); //wait until the configuration register is written 
</span><span class='line'>    
</span><span class='line'>    clk_high();
</span><span class='line'>    rst_high();
</span><span class='line'>    write_command(0xEE); //start conversion
</span><span class='line'>    rst_low();
</span><span class='line'>    delay(200);
</span><span class='line'>   
</span><span class='line'>    clk_high();
</span><span class='line'>    rst_high();
</span><span class='line'>    write_command(0xAA);
</span><span class='line'>    int raw_data = read_raw_data();
</span><span class='line'>    rst_low();
</span><span class='line'>    
</span><span class='line'>    mySerial.print("temperature:");
</span><span class='line'>    mySerial.print(raw_data/2);
</span><span class='line'>    mySerial.println(" C");
</span><span class='line'>    
</span><span class='line'>    
</span><span class='line'>    delay(100);
</span><span class='line'>  
</span><span class='line'>  
</span><span class='line'>  // toggle an LED just so you see the thing's alive.  
</span><span class='line'>  
</span><span class='line'>  toggle(13);
</span><span class='line'>
</span><span class='line'>}
</span><span class='line'>
</span><span class='line'>
</span><span class='line'>void toggle(int pinNum) {
</span><span class='line'>  // set the LED pin using the pinState variable:
</span><span class='line'>  digitalWrite(pinNum, pinState); 
</span><span class='line'>  // if pinState = 0, set it to 1, and vice versa:
</span><span class='line'>  pinState = !pinState;
</span><span class='line'>}
</span><span class='line'>
</span><span class='line'>void write_command(int command)
</span><span class='line'>/* sends 8 bit command on DQ output, least sig bit first */ 
</span><span class='line'>{
</span><span class='line'>  int n, bit;
</span><span class='line'>  
</span><span class='line'>  for(n=0;n&lt;8;n++)
</span><span class='line'>  {
</span><span class='line'>    bit = ((command >> n) & (0x01));
</span><span class='line'>    out_bit(bit);
</span><span class='line'>  }
</span><span class='line'>}
</span><span class='line'>
</span><span class='line'>int read_raw_data(void)
</span><span class='line'>{
</span><span class='line'>  int bit,n;
</span><span class='line'>  int raw_data=0;
</span><span class='line'>
</span><span class='line'>  pinMode(dqPin,INPUT);
</span><span class='line'>  
</span><span class='line'>     /* jam the dq lead high to use as input */
</span><span class='line'>  for(n=0;n&lt;9;n++)
</span><span class='line'>  {
</span><span class='line'>     clk_low();
</span><span class='line'>     bit=(digitalRead(dqPin));
</span><span class='line'>     clk_high();
</span><span class='line'>     raw_data = raw_data | (bit &lt;&lt;  n);
</span><span class='line'>  }
</span><span class='line'>  pinMode(dqPin, OUTPUT);
</span><span class='line'>  return(raw_data);
</span><span class='line'>}
</span><span class='line'>
</span><span class='line'>void out_bit(int bit) 
</span><span class='line'>{
</span><span class='line'>  digitalWrite(dqPin, bit);  /* set up the data */
</span><span class='line'>  clk_low();             /* and then provide a clock pulse */   
</span><span class='line'>  clk_high();
</span><span class='line'>}
</span><span class='line'>
</span><span class='line'>void clk_high(void)
</span><span class='line'>{
</span><span class='line'>  digitalWrite(clkPin,HIGH);
</span><span class='line'>}
</span><span class='line'>
</span><span class='line'>void clk_low(void)
</span><span class='line'>{
</span><span class='line'>  digitalWrite(clkPin,LOW);
</span><span class='line'>}
</span><span class='line'>
</span><span class='line'>void rst_high(void)
</span><span class='line'>{
</span><span class='line'>   digitalWrite(rstPin,HIGH);
</span><span class='line'>}
</span><span class='line'>
</span><span class='line'>void rst_low(void)
</span><span class='line'>{
</span><span class='line'>   digitalWrite(rstPin,LOW);
</span><span class='line'>}</span>

Java WebStart UnavailableServiceException

If you are getting javax.jnlp.UnavailableServiceException from javax.jnlp.ServiceManager.lookup() or if javax.jnlp.ServiceManager.getServicesNames returns null you must be aware that those call only return useful thing when you run your code from the Java Web Start environment.

It’s not enough to include the jnlp.jar or javaws.jar in the classpath.

For example to make it work from Netbeans you must enable WebStart in the project and Set Configuration to WebStart.

Copyright © 2015 - Ruben Laguna - Powered by Octopress