Quantcast
Channel: MSP430 Technical Forums
Viewing all articles
Browse latest Browse all 2077

MSP430G2 Launchpad and I2C Soil Moisture Sensor woes..

$
0
0

Dear all,

 

I have recently become interested in making things again and figured I'd automate my plant watering. I still had a MSP430G2 launchpad around and ordered a capacitive soil moisture sensor off tindie.com. The seller helpfully offers some example code to get the i2c interface up and running on an Arduino. So I figured I'd just connect the wires, run the sketch and get some moisture readings.. turns out I was wrong.

 

Here's how I connected things:

  • Pin 15 (P1_7) to "pin 4 - MOSI / SDA – I2C data"
  • Pin 14 (P1_6) to "pin 3 - SCK / SCL – I2C clock"
  • Pin 1 (VCC) to VCC
  • Pin 20 (GND) to GND

Nothing else, no resistors involved, no RST connected to anything. I'm running a version of the example code given by the seller. I put some println()s in to get some feedback over serial, which works initially. I also read that some devices need a lower bitrate set on the i2c bus, so I did play around with that to no avail.

 

The code hangs after the "loop" is printed, i.e. when the bus is to be read.

 

Any ideas on this? I'm slightly less incompetent on the programming side of things, but I have no idea how to hook this thing up electronically. So, a sanity check would be appreciated. Do I need a resistor somewhere? Take up knitting as a hobby? A loose wire on the breadboard perhaps?

#include <Wire.h>
//#include "msp430g2553.h"

void setup() {
  Serial.begin(9600);
  Serial.println("Starting up.");


  Wire.begin();
  // MSP430g2553 does not have an USI
  // It has an USCI.
#if defined(__MSP430_HAS_USCI__) || defined(__MSP430_HAS_USCI_B0__) || defined(__MSP430_HAS_USCI_B1__)
  // from twi.h and twi.c
  #define TWI_FREQ 12500L //100000L
  UCB0BR0 = (unsigned char)((F_CPU / TWI_FREQ) & 0xFF);
  UCB0BR1 = (unsigned char)((F_CPU / TWI_FREQ) >> 8);
  uint8_t clockRegister1 = UCB0BR0;
  uint8_t clockRegister2 = UCB0BR1;
  Serial.print("clk divider br0:");
  Serial.println(clockRegister1);
  Serial.print("clk divider br1:");
  Serial.println(clockRegister2);
#endif


}

void writeI2CRegister8bit(int addr, int value) {
  Wire.beginTransmission(addr);
  Wire.write(value);
  Wire.endTransmission();
}

unsigned int readI2CRegister16bit(int addr, int reg) {
  Wire.beginTransmission(addr);
  Wire.write(reg);
  Wire.endTransmission();
  delay(1100);
  Wire.requestFrom(addr, 2);
  unsigned int t = Wire.read() << 8;
  t = t | Wire.read();
  return t;
}

void loop() {
  Serial.println("Loop!");
  Serial.print(readI2CRegister16bit(0x20, 0)); //read capacitance register
  Serial.print(", ");
  Serial.print(readI2CRegister16bit(0x20, 5)); //temperature register
  Serial.print(", ");
  writeI2CRegister8bit(0x20, 3); //request light measurement 
  Serial.println(readI2CRegister16bit(0x20, 4)); //read light register
}




  • LaunchPad MSP430G2 revision 1.5
  • MSP430G2553
  • Energia 0101E0013
  • Arch Linux current 64bit

Viewing all articles
Browse latest Browse all 2077

Trending Articles