NotMortox
Posts: 4
Joined: Fri Dec 15, 2017 2:12 pm

Problem with voltage measure with MCP3424 in c++

Fri Dec 15, 2017 2:28 pm

Hey guys !

We want to get the voltage of our source with an MCP3424 using the i2c protocol between a raspberry pi3 ( the master ) and the MCP3424 ( the slave ).

The problem is that we dont know how to setup our MCP3424 with an c++ code.

Here is our code at the moment :

Code: Select all

#include <wiringPi.h>
#include <wiringPiI2C.h>
#include <stdio.h>
#include <iostream>
#include <mcp3422.h>

using namespace std;

#define ADC 0x6c
#define LED 27

int fd;
void Setup();

int main(void)
{
	wiringPiSetupGpio();
	pinMode(LED, OUTPUT);
	
	while(true)
	{
		Setup();
		delay(500);
	}
}

void Setup()
{
	mcp3422Setup(1, ADC, 240, 1);

	/*fd = wiringPiI2CSetup(ADC);

	digitalWrite(LED, HIGH);

	wiringPiI2CWrite(fd, 128);
	delay(4.1);*/

	digitalWrite(LED, LOW);

	int value = wiringPiI2CRead(fd);
	
	cout << value << endl;
}




As you can see we want to use the mcp3422setup functions from " wiringpi" library but we dont know how.

Before that , we tried with a basic write/read fuction ( comments part of the code ) , we got some data results who change if we change the intensity of our voltage source but these are totally wrong (Moving the source intensity from 0 to 3.3V and the display show a number from 0 to 7 ... Don't know what it means)

If you have any solutions , you're welcome because we are really stuck :|

Thanks

1dot0
Posts: 430
Joined: Mon Nov 28, 2016 12:31 pm

Re: Problem with voltage measure with MCP3424 in c++

Sun Dec 17, 2017 8:43 am

NotMortox wrote:
Fri Dec 15, 2017 2:28 pm
Hey guys !

We want to get the voltage of our source with an MCP3424 using the i2c protocol between a raspberry pi3 ( the master ) and the MCP3424 ( the slave ).

The problem is that we dont know how to setup our MCP3424 with an c++ code.

Here is our code at the moment :

Code: Select all

#include <wiringPi.h>
#include <wiringPiI2C.h>
#include <stdio.h>
#include <iostream>
#include <mcp3422.h>

using namespace std;

#define ADC 0x6c
#define LED 27

int fd;
void Setup();

int main(void)
{
	wiringPiSetupGpio();
	pinMode(LED, OUTPUT);
	
	while(true)
	{
		Setup();
		delay(500);
	}
}

void Setup()
{
	mcp3422Setup(1, ADC, 240, 1);

	/*fd = wiringPiI2CSetup(ADC);

	digitalWrite(LED, HIGH);

	wiringPiI2CWrite(fd, 128);
	delay(4.1);*/

	digitalWrite(LED, LOW);

	int value = wiringPiI2CRead(fd);
	
	cout << value << endl;
}




As you can see we want to use the mcp3422setup functions from " wiringpi" library but we dont know how.

Before that , we tried with a basic write/read fuction ( comments part of the code ) , we got some data results who change if we change the intensity of our voltage source but these are totally wrong (Moving the source intensity from 0 to 3.3V and the display show a number from 0 to 7 ... Don't know what it means)

If you have any solutions , you're welcome because we are really stuck :|

Thanks
I agree, Raspi C/C++ is by far not as easy to use as Arduino libs. But here I found a website providing source code both for Arduino (C++) and Raspberry Pi, and they also link to a github rep for the MCP chip (C language):
https://www.dfrobot.com/wiki/index.php/ ... U:DFR0316)
https://github.com/angeloc/linux_iio_mcp3422

HTH!

User avatar
[email protected]
Posts: 1979
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: Problem with voltage measure with MCP3424 in c++

Sun Dec 17, 2017 12:50 pm

NotMortox wrote:
Fri Dec 15, 2017 2:28 pm
Hey guys !

We want to get the voltage of our source with an MCP3424 using the i2c protocol between a raspberry pi3 ( the master ) and the MCP3424 ( the slave ).

The problem is that we dont know how to setup our MCP3424 with an c++ code.
There are examples online and there's always the source code - which IMO should have been your first port of call. It's all at http://wiringpi.com/ however...

Code: Select all

  mcp3424Setup (base, i2cAddr, sampleRate, gain) ;
is the setup call - base is the new pin base you wish to allocate for the device (any number >= 64 - this is the new pin number you invent for the device - wiringPi is a pin based gpio lbirary), i2caddr is the devices i2caddress (probably 0x6C here) sampleRate and gain values can be found in the .h file - typically 0 for 240 and 0 for gain 1.

then you simply:

Code: Select all

  value = analogRead (base) ;
and off you go. Note that as this is a 4-channel device, you have 4 new pins; base+0, base+1, base+2 and base+3 for the 4 new pins representing analog inputs 0 through 3.

-Gordon
--
Gordons projects: https://projects.drogon.net/

1dot0
Posts: 430
Joined: Mon Nov 28, 2016 12:31 pm

Re: Problem with voltage measure with MCP3424 in c++

Sun Dec 17, 2017 2:05 pm

hello Gordon,
I also have been looking through your websites before I suggested the other link above, both wiringPi and Projects, just because I was curious, but none of your websites listed the MCP3424, neither beneath Examples/HowTo, nor beneath Reference, nor beneath Extensions, nor beneath DevLib -
all I found was MCP23017, MCP23008, PCF8574, PCF8591 and a couple more.

Also the search function on your website doesn't find it:
Search Results MCP3424
Nothing Found
Nothing Found
Sorry, but nothing matched your search criteria. Please try again with some different keywords.
Where is the overview which lists all and everything?

User avatar
[email protected]
Posts: 1979
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: Problem with voltage measure with MCP3424 in c++

Sun Dec 17, 2017 3:43 pm

Where is the overview which lists all and everything?
It's all in the source code.

I just haven't had the time/energy to document everything due to other issues/illness in the past few years, sorry.

-Gordon
--
Gordons projects: https://projects.drogon.net/

1dot0
Posts: 430
Joined: Mon Nov 28, 2016 12:31 pm

Re: Problem with voltage measure with MCP3424 in c++

Sun Dec 17, 2017 3:50 pm

[email protected] wrote:
Sun Dec 17, 2017 3:43 pm
Where is the overview which lists all and everything?
It's all in the source code.

I just haven't had the time/energy to document everything due to other issues/illness in the past few years, sorry.

-Gordon

source code? where?
when I search, I use my Windows PC and my Firefox browser for the internet, I can't use my Pi for that (far too small screen, far too slow internet).
searching in the web is most probably what everyone does when searching for libs and drivers. So it would be very precious if you could list/upload your code sources in the web, perhaps on github or sourceforge net.
Otherwise it would be almost impossible to find anything of your work and then to use it eventually, unfortunately.

User avatar
rpdom
Posts: 12754
Joined: Sun May 06, 2012 5:17 am
Location: Ankh-Morpork

Re: Problem with voltage measure with MCP3424 in c++

Sun Dec 17, 2017 4:33 pm

1dot0 wrote:
Sun Dec 17, 2017 3:50 pm
source code? where?
https://git.drogon.net/
There.

User avatar
[email protected]
Posts: 1979
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: Problem with voltage measure with MCP3424 in c++

Sun Dec 17, 2017 4:40 pm

1dot0 wrote:
Sun Dec 17, 2017 3:50 pm
[email protected] wrote:
Sun Dec 17, 2017 3:43 pm
Where is the overview which lists all and everything?
It's all in the source code.

I just haven't had the time/energy to document everything due to other issues/illness in the past few years, sorry.

-Gordon

source code? where?
The link I gave earlier - http://wiringpi.com/

See the download/install link.-Gordon
--
Gordons projects: https://projects.drogon.net/

1dot0
Posts: 430
Joined: Mon Nov 28, 2016 12:31 pm

Re: Problem with voltage measure with MCP3424 in c++

Sun Dec 17, 2017 5:11 pm

rpdom wrote:
Sun Dec 17, 2017 4:33 pm
1dot0 wrote:
Sun Dec 17, 2017 3:50 pm
source code? where?
https://git.drogon.net/
There.
where is the MCP3424 listed in https://git.drogon.net/ ? and perhaps even more drivers?
[email protected] wrote:
Sun Dec 17, 2017 4:40 pm
The link I gave earlier - http://wiringpi.com/
See the download/install link.-Gordon
http://wiringpi.com/download-and-install/
:?:
where can I browse this with my Windows PC? How can Google find the MCP3424 in there?

User avatar
[email protected]
Posts: 1979
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: Problem with voltage measure with MCP3424 in c++

Sun Dec 17, 2017 5:15 pm

1dot0 wrote:
Sun Dec 17, 2017 5:11 pm

where can I browse this with my Windows PC? How can Google find the MCP3424 in there?
I'm sorry. I don't think I can help you further.

-Gordon
--
Gordons projects: https://projects.drogon.net/

1dot0
Posts: 430
Joined: Mon Nov 28, 2016 12:31 pm

Re: Problem with voltage measure with MCP3424 in c++

Sun Dec 17, 2017 5:19 pm

that's a pity, because it would help a lot if Google on Windows could find your ressources, too!
That's exactly why always people ask here in this forum about drivers, although wiringPi provides them already - but they don't find anything!

User avatar
DougieLawson
Posts: 33630
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Problem with voltage measure with MCP3424 in c++

Sun Dec 17, 2017 5:26 pm

1dot0 wrote:
Sun Dec 17, 2017 5:19 pm
that's a pity, because it would help a lot if Google on Windows could find your ressources, too!
That's exactly why always people ask here in this forum about drivers, although wiringPi provides them already - but they don't find anything!
https://git.drogon.net/?p=wiringPi;a=bl ... ed;hb=HEAD
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

1dot0
Posts: 430
Joined: Mon Nov 28, 2016 12:31 pm

Re: Problem with voltage measure with MCP3424 in c++

Sun Dec 17, 2017 5:38 pm

DougieLawson wrote:
Sun Dec 17, 2017 5:26 pm
1dot0 wrote:
Sun Dec 17, 2017 5:19 pm
that's a pity, because it would help a lot if Google on Windows could find your ressources, too!
That's exactly why always people ask here in this forum about drivers, although wiringPi provides them already - but they don't find anything!
https://git.drogon.net/?p=wiringPi;a=bl ... ed;hb=HEAD
the question is: can Google find that?
and what else of all this may also Google find? https://git.drogon.net/?p=wiringPi;a=tr ... Pi;hb=HEAD
(at least I didn't find MCP3424 in wiringPi by Google, instead https://www.dfrobot.com/wiki/index.php/ ... DFR0316%29 was 1 of the first matches)

User avatar
DougieLawson
Posts: 33630
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Problem with voltage measure with MCP3424 in c++

Sun Dec 17, 2017 8:02 pm

Google can't find it because Gordon could be blocking the Googlebots on his small and overloaded website.

Also you need a special level of Google Fu to find things like that (mainly because it doesn't contain "MCP3424" as a plain text string).
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

User avatar
[email protected]
Posts: 1979
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: Problem with voltage measure with MCP3424 in c++

Sun Dec 17, 2017 8:11 pm

DougieLawson wrote:
Sun Dec 17, 2017 8:02 pm
Google can't find it because Gordon could be blocking the Googlebots on his small and overloaded website.

Also you need a special level of Google Fu to find things like that (mainly because it doesn't contain "MCP3424" as a plain text string).
I'm not blocking anything. Google can't find it simply because it isn't listed in a page on my website, (I've not written documentation for reasons already written which the OP seems to have glossed over), however the OP (now in my 'foe' list) knew it was in wiringPi but was unable to find any references to it, or the source code simply because they were relying on using "google on windows" rather than actually thinking about the issue in-hand and making an effort to look for the source code, even after being given links to it three times.

-Gordon
--
Gordons projects: https://projects.drogon.net/

1dot0
Posts: 430
Joined: Mon Nov 28, 2016 12:31 pm

Re: Problem with voltage measure with MCP3424 in c++

Sun Dec 17, 2017 8:48 pm

[email protected] wrote:
Sun Dec 17, 2017 8:11 pm
DougieLawson wrote:
Sun Dec 17, 2017 8:02 pm
Google can't find it because Gordon could be blocking the Googlebots on his small and overloaded website.

Also you need a special level of Google Fu to find things like that (mainly because it doesn't contain "MCP3424" as a plain text string).
I'm not blocking anything. Google can't find it simply because it isn't listed in a page on my website, (I've not written documentation for reasons already written which the OP seems to have glossed over), however the OP (now in my 'foe' list) knew it was in wiringPi but was unable to find any references to it, or the source code simply because they were relying on using "google on windows" rather than actually thinking about the issue in-hand and making an effort to look for the source code, even after being given links to it three times.

-Gordon
Gordon, are you serious? and whom did you block or "add to a foe list"? me? I am not the OP, I personally don't need a MCP3424 lib at all, and tbh, I don't even care about a MCP3424 lib.
And I googled just in order to help the OP, as no one cared about his question for some days - no idea that this thing was in any wringpi rep or folder already!
Nonetheless, I can imagine why the OP couldn't find a wiringPi lib about it, although he already was using and #including wiringPi - probably because of the same reason why I also coudn't find it when searching libs for it:
- because Google search coudn't find it and no hints about it are given on your website, that simply is my point!

User avatar
PeterO
Posts: 4237
Joined: Sun Jul 22, 2012 4:14 pm

Re: Problem with voltage measure with MCP3424 in c++

Sun Dec 17, 2017 9:33 pm

A couple of minutes with google .....

Google for MCP3424 take link to microchip site (the makers of the chip).

http://www.microchip.com/wwwproducts/en/MCP3424

Click the "Documentation" link on that page and take link to "MCP3422/3/4 Data Sheet" OH :shock: Here's a clue, the MCP3422,3423 and 3424 share a data sheet.... Maybe they are similar chips ?

Looking at data sheet confirms they are the same chips with different numbers of inputs, so maybe they share the same driver code ?

Look in wiringPi source code and low and behold what do we find...

Code: Select all

/*
 * mcp3422.c:
 *      Extend wiringPi with the MCP3422/3/4 I2C ADC chip
 *      This code assumes single-ended mode only.
 *      Tested on actual hardware: 20th Feb 2016.
 *      Copyright (c) 2013-2016 Gordon Henderson
 ***********************************************************************
 * This file is part of wiringPi:
 *      https://projects.drogon.net/raspberry-pi/wiringpi/
So that answer was there all along, but if you are too lazy to look where you are told then it's not surprising that Gordon has got fed up with trying to help you. :o

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),Aeromodelling,1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
DougieLawson
Posts: 33630
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Problem with voltage measure with MCP3424 in c++

Sun Dec 17, 2017 9:42 pm

[email protected] wrote:
Sun Dec 17, 2017 8:11 pm
however the OP (now in my 'foe' list) knew it was in wiringPi but was unable to find any references to it, or the source code simply because they were relying on using "google on windows" rather than actually thinking about the issue in-hand and making an effort to look for the source code, even after being given links to it three times.
I use "Google on Windows" that appears to be identical to "Google on Linux". One Google.

The easy way was to scrabble round your Gitweb to find the offending modules. It wasn't hard work, but as Peter Onion surmises I had a tactical advantage by knowing that the MCP342x chips are all similar.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

NotMortox
Posts: 4
Joined: Fri Dec 15, 2017 2:12 pm

Re: Problem with voltage measure with MCP3424 in c++

Sun Dec 17, 2017 9:47 pm

PeterO wrote:
Sun Dec 17, 2017 9:33 pm
A couple of minutes with google .....

Google for MCP3424 take link to microchip site (the makers of the chip).

http://www.microchip.com/wwwproducts/en/MCP3424

Click the "Documentation" link on that page and take link to "MCP3422/3/4 Data Sheet" OH :shock: Here's a clue, the MCP3422,3423 and 3424 share a data sheet.... Maybe they are similar chips ?

Looking at data sheet confirms they are the same chips with different numbers of inputs, so maybe they share the same driver code ?

Look in wiringPi source code and low and behold what do we find...

Code: Select all

/*
 * mcp3422.c:
 *      Extend wiringPi with the MCP3422/3/4 I2C ADC chip
 *      This code assumes single-ended mode only.
 *      Tested on actual hardware: 20th Feb 2016.
 *      Copyright (c) 2013-2016 Gordon Henderson
 ***********************************************************************
 * This file is part of wiringPi:
 *      https://projects.drogon.net/raspberry-pi/wiringpi/
So that answer was there all along, but if you are too lazy to look where you are told then it's not surprising that Gordon has got fed up with trying to help you. :o

PeterO



Thanks for that Mr.Santa ,

But if you wasn't too lazy to see the code that i send on that post , you can see that we already tried to use the mcp3422 library for our MCP3424 but without success....this is why i ask some help , it's because i got problems with this library when i tried to use it.

So don't be so arrogant pls ;)

Kind regards

NotMortox
Posts: 4
Joined: Fri Dec 15, 2017 2:12 pm

Re: Problem with voltage measure with MCP3424 in c++

Sun Dec 17, 2017 9:48 pm

[email protected] wrote:
Sun Dec 17, 2017 12:50 pm
NotMortox wrote:
Fri Dec 15, 2017 2:28 pm
Hey guys !

We want to get the voltage of our source with an MCP3424 using the i2c protocol between a raspberry pi3 ( the master ) and the MCP3424 ( the slave ).

The problem is that we dont know how to setup our MCP3424 with an c++ code.
There are examples online and there's always the source code - which IMO should have been your first port of call. It's all at http://wiringpi.com/ however...

Code: Select all

  mcp3424Setup (base, i2cAddr, sampleRate, gain) ;
is the setup call - base is the new pin base you wish to allocate for the device (any number >= 64 - this is the new pin number you invent for the device - wiringPi is a pin based gpio lbirary), i2caddr is the devices i2caddress (probably 0x6C here) sampleRate and gain values can be found in the .h file - typically 0 for 240 and 0 for gain 1.

then you simply:

Code: Select all

  value = analogRead (base) ;
and off you go. Note that as this is a 4-channel device, you have 4 new pins; base+0, base+1, base+2 and base+3 for the 4 new pins representing analog inputs 0 through 3.

-Gordon
Thanks for your reply :) , but when i try to use a pinbase (like 200) i got an "overlaps" message in output ...i don't understand the meaning of this , can you help me ?

User avatar
PeterO
Posts: 4237
Joined: Sun Jul 22, 2012 4:14 pm

Re: Problem with voltage measure with MCP3424 in c++

Sun Dec 17, 2017 10:01 pm

NotMortox wrote:
Sun Dec 17, 2017 9:47 pm

Thanks for that Mr.Santa ,

But if you wasn't too lazy to see the code that i send on that post ,
No I read you post, I can see what you were doing!

You are not the person who is complaining about not being able to find things on Gordon's site, my comments about lazyness were not aimed at you ;)

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),Aeromodelling,1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

1dot0
Posts: 430
Joined: Mon Nov 28, 2016 12:31 pm

Re: Problem with voltage measure with MCP3424 in c++

Mon Dec 18, 2017 8:46 am

PeterO wrote:
Sun Dec 17, 2017 9:33 pm
A couple of minutes with google .....

Google for MCP3424 take link to microchip site (the makers of the chip).

http://www.microchip.com/wwwproducts/en/MCP3424

Click the "Documentation" link on that page and take link to "MCP3422/3/4 Data Sheet" OH :shock: Here's a clue, the MCP3422,3423 and 3424 share a data sheet.... Maybe they are similar chips ?

Looking at data sheet confirms they are the same chips with different numbers of inputs, so maybe they share the same driver code ?

Look in wiringPi source code and low and behold what do we find...

Code: Select all

/*
 * mcp3422.c:
 *      Extend wiringPi with the MCP3422/3/4 I2C ADC chip
 *      This code assumes single-ended mode only.
 *      Tested on actual hardware: 20th Feb 2016.
 *      Copyright (c) 2013-2016 Gordon Henderson
 ***********************************************************************
 * This file is part of wiringPi:
 *      https://projects.drogon.net/raspberry-pi/wiringpi/
So that answer was there all along, but if you are too lazy to look where you are told then it's not surprising that Gordon has got fed up with trying to help you. :o

PeterO
I was not the one who needed help,
I already found a driver which is supposed to work and to which I already have linked,
but I pointed out to the fact that the MCP3424 ist not explicitely listed on the wiringPi websites what actually would have been expected - and that not even Google search finds it i an original G.H. wiringPi rep or whatever.
So eventually not even Gordon's statement
[email protected] wrote:The link I gave earlier - http://wiringpi.com/
was true, because it's simply not there.
After all, as I was the first one who kindly tried to help at all after 2 days of silence, without having a proprietary interest in having that driver, I honestly cannot recognize why TF you accuse me of being "lazy"?
OTOH, if you, Peter, found that information so easily, why have you been so lazy not to reply earlier? Instead you're now wise after the event, and you made your Google search just for arguing reasons, just to claim how easy it was allegedly: that is utterly rediculous.

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 10869
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Problem with voltage measure with MCP3424 in c++

Mon Dec 18, 2017 10:26 am

Finding info on the net is a question of having horse sense, its an art, not simply the ability to enter a search string into google.
Its also not always the responsibility of the writer of the driver to find out, and list, all devices his driver is going to work with, because its also the responsibility of the user that is going to use the driver to look into datasheets and such to see if this is the case for his particular chip.
So you cannot simply rely on a text search, you have to use your mind.

Also, the level of politeness here is quickly declining, remember that we are all volunteers here.

Keep it polite, or I might need to intervene. :roll:

Return to “C/C++”

Who is online

Users browsing this forum: No registered users and 8 guests