Would you like to react to this message? Create an account in a few clicks or log in to continue.

You are not connected. Please login or register

Arduino Sensor help

3 posters

Go down  Message [Page 1 of 1]

1Arduino Sensor help Empty Arduino Sensor help Sun Apr 04, 2021 9:36 pm

feri



Hello
I would like to try to create sensors for arduino, I made some for proteus and I wanted to try with simulIDE which I really liked.
I know a little C ++ and would like a tutorial to create if available
new animated components in simulIDE.
Sincerely
Giordano

2Arduino Sensor help Empty Re: Arduino Sensor help Sun Apr 04, 2021 10:45 pm

arcachofo

arcachofo

Hi Giordano.

There is no tutorial to create sensors.
Depending on the sensor it could be created in different ways.

Which sensor do you want to create?

3Arduino Sensor help Empty Re: Arduino Sensor help Mon Apr 05, 2021 8:47 pm

feri



I would like to create some simple sensors like the sr04
i saw the sr04.cpp and sr04.h files.
For the moment I would try with simple sensors based mostly on graphics but I would need at least a list of functions
available with a little explanation in any language.
Mainly circuits for Arduino type:

https://www.ebay.it/itm/sensore-umidita-terreno-Igrometro-suolo-per-Arduino-Pic/173862822641?hash=item287b0822f1:g:t18AAOSwVotcpjWm

https://www.ebay.it/itm/2PZ-MODULO-SENSORE-DI-SUONO-RILEVAMENTO-VOCE-RUMORE-AMBIENTALE-MICROFONO-ARDUINO/191540974387?hash=item2c98bb5333:g:1VIAAOSwpO5deoxP

https://www.ebay.it/itm/2PZ-MODULO-SENSORE-DI-PROSSIMITA-INFRAROSSO-LED-IR-RILEVATORE-OSTACOLO-ARDUINO/191791601660?hash=item2ca7ab97fc:g:kZsAAOSwquRdfPeH

Best regards
Giordano

4Arduino Sensor help Empty Re: Arduino Sensor help Mon Apr 05, 2021 9:25 pm

arcachofo

arcachofo

I would like to create some simple sensors like the sr04
i saw the sr04.cpp and sr04.h files.
Yes, SR04 is already implemented.

For sensors like humidity sensors, I think you can just use variable resistors.
To add some graphical representation you should subclass Variable resistor and reimplement Paint() function.
But it depends on how you want it to work, maybe you want to set humidity percentage instead of the resistance directly.
For this, have a look at other resistive sensors like LDR.

For IR proximity sensor, depends on how you want it to work. As far as I know, it is just an on/off digital signal, so maybe you just want to subclass "Fixed Volt" component.

For the microphone, if you want to get audio from the computer, currently there is no audio input in simulide.
You should implement the audio part.

What I would do:
Start with the simplest one, perhaps the humidity sensor.
Determine exactly what you want it to do.
Have a look at LDR component, copy it, renaming files and classes and reimplement whatever you need.

If you get into problems or don't know how to do something, don't hesitate to ask.

Aejwt likes this post

5Arduino Sensor help Empty Re: Arduino Sensor help Thu Apr 08, 2021 7:53 pm

feri



I installed Qt version: 5.15.2 on UBUNTU 20.04 and tried to compile the sources: simulide_0.4.15-Final_Sources.zip
but I get the following error in Qt issues tab:

Project ERROR: Unknown module(s) in QT: script

Searching the internet for qt script, it is deprecated
and I can't figure out if it's a related problem
Qt installation or project configuration.
If you can help me, thank you.
kind regards
Giordano

6Arduino Sensor help Empty Re: Arduino Sensor help Thu Apr 08, 2021 8:52 pm

arcachofo

arcachofo

Project ERROR: Unknown module(s) in QT: script
Install libqt5script5 and qtscript5-dev:

sudo apt-get install libqt5script5 qtscript5-dev

If you can't install these libs let me know.

7Arduino Sensor help Empty Re: Arduino Sensor help Tue Apr 13, 2021 2:21 pm

feri



Hello
I managed to compile but I get 45 errors.
In the version I compiled some example files lose their connections, others do not finish mostly I2C display, and if the language is Italian the side menu messes up a bit.
Here are some of the errors:

In file included from ../src/gui/circuitwidget/components/meters/oscopechannel.cpp:25:
../src/gui/circuitwidget/components/meters/oscopechannel.cpp: In member function ‘void OscopeChannel::updateValues()’:
../src/gui/circuitwidget/components/meters/oscopechannel.cpp:71:36: warning: macro expands to multiple statements [-Wmultistatement-macros]
  71 |     if( val >= 1 ) valToUnit( val, unit, decs );
     |                                    ^~~~
../src/utils.h:38:5: note: in definition of macro ‘valToUnit’
  38 |     mult = " p";\
     |     ^~~~
../src/gui/circuitwidget/components/meters/oscopechannel.cpp:71:5: note: some parts of macro expansion are not guarded by this ‘if’ clause
  71 |     if( val >= 1 ) valToUnit( val, unit, decs );
     |     ^~
In file included from ../src/gui/circuitwidget/components/meters/oscopechannel.cpp:25:
../src/gui/circuitwidget/components/meters/oscopechannel.cpp:76:36: warning: macro expands to multiple statements [-Wmultistatement-macros]
  76 |     if( val >= 1 ) valToUnit( val, unit, decs );
     |                                    ^~~~
../src/utils.h:38:5: note: in definition of macro ‘valToUnit’
  38 |     mult = " p";\
     |     ^~~~
../src/gui/circuitwidget/components/meters/oscopechannel.cpp:76:5: note: some parts of macro expansion are not guarded by this ‘if’ clause
  76 |     if( val >= 1 ) valToUnit( val, unit, decs );
     |     ^~
     |          ^~~~~

I enclose the build report:
Attachments
Arduino Sensor help Attachmentbuild.zip
You don't have permission to download attachments.
(29 Kb) Downloaded 1 times

8Arduino Sensor help Empty Re: Arduino Sensor help Tue Apr 13, 2021 8:44 pm

arcachofo

arcachofo

I managed to compile but I get 45 errors.
I think those are warnings, not errors, most of them about deprecated functions in Qt.
But this should not be a problem.

9Arduino Sensor help Empty Re: Arduino Sensor help Fri Apr 30, 2021 10:19 pm

Mistral



It depends on what kind of IR is used. I am doing some sensor projects and for example when an IR-led is used as obstacle detector or as line follower or as a Pir it is just giving a On or Off signal, easily to be replaced by a 5Volt button. 
https://circuitdigest.com/microcontroller-projects/line-follower-robot-using-pic16f877a
https://circuitdigest.com/microcontroller-projects/obstacle-avoiding-robot-using-pic16f877a
https://circuitdigest.com/microcontroller-projects/pir-sensor-interfacing-with-pic16f877a
IR is also used as remote control and that gives train pulses
https://circuitdigest.com/microcontroller-projects/ir-remote-controlled-home-automation-using-pic
I assume this requires a very good understanding of the program code that is used and of what kind of signal is sent out before it can be simulated, it goes a little bit too deep for me to dive into for many hours although it is not completely new for me, I've made it before with a real one, with an Arduino.
Tried it again with temperature sensor DS18B20, first with just a generator on the uC pin-input, later with 74HC166 Piso shift register, back again to generator, changing frequencies and waveforms and hoping for luck it would catch. But if you look at the datasheet than for me the output of the sensor is pretty complex, I see for example a Presence Detect signal so nothing much happened in this simulation. 

https://cdn.sparkfun.com/datasheets/Sensors/Temp/DS18B20.pdf

Arduino Sensor help Cd27_t10

https://circuitdigest.com/microcontroller-projects/pic16f877a-digital-thermometer-using-ds18b20

Learned from it and it's fun trying. Next one will be better and some other sensors are pretty much by the same principle, they put some voltage on the Adc pin of the micro and that gives an output to a display or 7-segment etc. Such sensors can be replaced by a Volt source or potmeter.

And there are already some sensors available to try, like the ultrasonic one, the joystick KY-023 and the Rotary encoder KY-040. 

And I suppose you can program any 'sensor' output signal you want with a second micro-controller. How to connect these two avoiding the 'only one micro' -rule I have not tried yet. But is seems to be possible as shown in this video at minute 05:00.
https://www.youtube.com/watch?v=WVBgmah6gE0

10Arduino Sensor help Empty Re: Arduino Sensor help Sat May 01, 2021 12:34 am

feri



Thanks for the replies.
I am trying to understand I2C using I2C memory as an example.
I have not been able to write on it and I can only read the
address 0x00.
I tried to make an I2C device I can receive from AVR
but I cannot transmit the data.
On the AVR side I use the following request:
   
   #define ADDRESS 80
   Wire.beginTransmission(ADDRESS);
   Wire.write(zero);
   Wire.endTransmission();
   Wire.requestFrom(ADDRESS, 7);
   if (Wire.available())
   {
       Serial.println(Wire.read(), HEX);
       Serial.println(Wire.read(), HEX);
       Serial.println(Wire.read(), HEX);
       Serial.println(Wire.read(), HEX);
       Serial.println(Wire.read(), HEX);
       Serial.println(Wire.read(), HEX);
       Serial.println(Wire.read(), HEX);
       Serial.println();
  }

While with the I2C memory I used the attached code.
With best regards
Giordano
Attachments
Arduino Sensor help Attachmentread_and_write.zip
You don't have permission to download attachments.
(1 Kb) Downloaded 2 times

11Arduino Sensor help Empty Re: Arduino Sensor help Sat May 01, 2021 1:52 pm

arcachofo

arcachofo

@feri

Your read_and_write.ino code works for Memories < 256 bytes.
So first you should set RAM size to 255.

For sizes > 255 you should use 2 bytes for address.
This is an example sketch for I2C 24LC256 eeprom:
Code:
#include <Wire.h>    
 
#define i2caddr 80    //Address of 24LC256 eeprom chip
 
void setup(void)
{
    Serial.begin( 9600 );
    Wire.begin();  
    
    writeEEPROM( i2caddr, 0, 0x0F );
    Serial.print( readEEPROM( i2caddr, 0 ), DEC );
    Serial.print( "\n" );
}
 
void loop()
{
}
 
void writeEEPROM( int deviceaddress, unsigned int eeaddress, byte data )
{
  Wire.beginTransmission( deviceaddress );
  Wire.write( eeaddress >> 8 );   // MSB
  Wire.write( eeaddress & 0xFF ); // LSB

  Wire.write( data );
  Wire.endTransmission();
 
  delay(5);
}
 
byte readEEPROM( int deviceaddress, unsigned int eeaddress )
{
  byte rdata = 0;
 
  Wire.beginTransmission(deviceaddress);
  Wire.write( eeaddress >> 8 );   // MSB
  Wire.write( eeaddress & 0xFF ); // LSB
  Wire.endTransmission();
 
  Wire.requestFrom( deviceaddress, 1 );
 
  if( Wire.available() )
    rdata = Wire.read();
    
  return rdata;
}

12Arduino Sensor help Empty Re: Arduino Sensor help Sat May 01, 2021 2:29 pm

Mistral



Hi Feri,
I am new to this I2C doing, or it's such a long time ago I forgot. I downloaded your zip and put it in the Uno. I used Notepad++ to upload data into the Ram and I changed line 3 in your code to a hex-number 
#define ADDR 0X50
but probably that would not have mattered. In 0.4.14 the serial monitor showed nothing but 0.4.15 gives this:

Arduino Sensor help Uno_i210

It means the Uno is communicating data with the Ram. Writing I have not tried yet, like I said it's new to me and takes time. However I was already some time interested in this I2C ram to test so your post is a good opportunity to start with, thanks.
The Uno is the non-experimental version.
The text-line 'Device found' comes from this webpage
https://create.arduino.cc/projecthub/abdularbi17/how-to-scan-i2c-address-in-arduino-eaadda

May I suggest to keep it as simple as possible, start with only the most necessary and most basic commands to see if it's working. Me myself I am very rusty with coding.
I will try to write and if succeed I will let you know.  Smile

Ps, set the Ram to Persistent true while testing if you have not done this yet or data will be lost on every Start.

13Arduino Sensor help Empty Re: Arduino Sensor help Sat May 01, 2021 3:22 pm

Mistral



@arcachofo
Do you also have different behaviour when you try it in 0.4.14-SR4?
It's stuck here for 10 minutes and the wires stay blue with the same scan program as above. Also feri's zip gives no response on the 4.14 serial monitor?

Arduino Sensor help Uno_i211


******************

Edit: It's working now

Arduino Sensor help Uno_i212

These pull-ups seem to be not necessary in 4.15?
I saw it in this article
https://howtomechatronics.com/tutorials/arduino/how-i2c-communication-works-and-how-to-use-it-with-arduino/

14Arduino Sensor help Empty Re: Arduino Sensor help Sat May 01, 2021 9:51 pm

Mistral



It would have been very difficult if arcachofo had not mentioned that the size of the Eeprom should be 255. Still don't understand a lot of it but his example program worked with the size-adjustment. I found one where you can leave the size as it is, at 65536 and it works in 4.14 and 4.15.
The characters shown are a little bit different than in the program. The program uses some old syntax, it is here 
https://www.robotshop.com/media/files/pdf/arduino-tutorial-dfr0117.pdf
this one is with new syntax after I find/replaced Write.send and Write.receive.

Code:

/*
 * Use the I2C bus with EEPROM 24LC64
 * Sketch: eeprom.pde
 *
 * Author: hkhijhe
 * Date: 01/10/2010
 *
 *
 */
 #include <Wire.h> //I2C library
 void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, byte data ) {
 int rdata = data;
 Wire.beginTransmission(deviceaddress);
 Wire.write((int)(eeaddress >> 8)); // MSB
 Wire.write((int)(eeaddress & 0xFF)); // LSB
 Wire.write(rdata);
 Wire.endTransmission();
 }
 // WARNING: address is a page address, 6-bit end will wrap around
 // also, data can be maximum of about 30 bytes, because the Wire library has a buffer of 32 bytes
 void i2c_eeprom_write_page( int deviceaddress, unsigned int eeaddresspage, byte* data, byte length )
{
 Wire.beginTransmission(deviceaddress);
 Wire.write((int)(eeaddresspage >> 8)); // MSB
 Wire.write((int)(eeaddresspage & 0xFF)); // LSB
 byte c;
 for ( c = 0; c < length; c++)
 Wire.write(data[c]);
 Wire.endTransmission();
 }
 byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress ) {
 byte rdata = 0xFF;
 Wire.beginTransmission(deviceaddress);
 Wire.write((int)(eeaddress >> 8)); // MSB
 Wire.write((int)(eeaddress & 0xFF)); // LSB
 Wire.endTransmission();
 Wire.requestFrom(deviceaddress,1);
 if (Wire.available()) rdata = Wire.read();
 return rdata;
 }
 // maybe let's not read more than 30 or 32 bytes at a time!
 void i2c_eeprom_read_buffer( int deviceaddress, unsigned int eeaddress, byte *buffer, int length ) {
 Wire.beginTransmission(deviceaddress);
 Wire.write((int)(eeaddress >> 8)); // MSB
 Wire.write((int)(eeaddress & 0xFF)); // LSB
 Wire.endTransmission();
 Wire.requestFrom(deviceaddress,length);
 int c = 0;
 for ( c = 0; c < length; c++ )
 if (Wire.available()) buffer[c] = Wire.read();
 }
 void setup()
 {
 char somedata[] = "this is data from the eeprom"; // data to write
 Wire.begin(); // initialise the connection
 Serial.begin(9600);
 i2c_eeprom_write_page(0x50, 0, (byte *)somedata, sizeof(somedata)); // write to EEPROM
 delay(1000); //add a small delay
 Serial.println("Memory written");
 }
 void loop()
 {
 int addr=0; //first address
 byte b = i2c_eeprom_read_byte(0x50, 0); // access the first address from the memory
 while (b!=0)
 {
 Serial.print((char)b); //print content to serial port
 addr++; //increase address
 b = i2c_eeprom_read_byte(0x50, addr); //access an address from the memory
 }
 Serial.println(" ");
 delay(2000);
 }
 



Arduino Sensor help Eeprom11

Though subject this. I tried this but is not working
https://www.arduino.cc/en/Reference/WireWrite
I figure special Eeprom-commands are needed like this as arcachofo has shown
void writeEEPROM( int deviceaddress, unsigned int eeaddress, byte data )
I pasted it in the browser and that led me to more related topics like the robotshop above. I do not understand all the code and don't know why the characters don't match 100% but I hope you do and that this is helpfull.  Wink

edit:
The letters in the ram-memory table do match up but not on the serial monitor?


Arduino Sensor help Eeprom12

edit2:
It works in 4.14 but only with pull-up resistors as shown in previous post.
edit3: Sparkfun tutorial with 24LC256
https://learn.sparkfun.com/tutorials/reading-and-writing-serial-eeproms/all

15Arduino Sensor help Empty Re: Arduino Sensor help Sun May 02, 2021 11:42 am

Mistral



Sorry about the series of post but sometimes I get carried away with enthousiasm  drunken
It's a nice subject and finally am beginning to grasp it a little bit, thank you Feri to bring this under attention. 
@Arcachofo, I tried your example code again with these lines:

void setup(void)
{
    Serial.begin( 9600 );
    Wire.begin();  
    
    writeEEPROM( i2caddr, 0x4D6, 0xC5 );
    Serial.print( readEEPROM( i2caddr, 0x4D6 ), DEC );
    Serial.print( "\n" );
}

It writes C5 (197) into Ram-adress 4D6 and that's working good. When I take a look at the memory table it looks like this:

Arduino Sensor help Memory10

What I am trying to say is that it's a little bit difficult to find the address back because it does not show 4D0 but D0 in the left column. I am very glad with these memory tables, it's so much better than every time opening and looking in Notepad++ and I used it a lot already with the 'normal' memory components thank you very much and I am just sharing my findings. Another one was that Simulide 4.15 crashed repeatedly yesterday when trying to open the memory table. One time it was so bad I had to delete the ram and place a 'fresh' one to get it going again.

@Feri
I came accross another two webpages that I find gives a good overview of this subject.
https://www.hobbytronics.co.uk/arduino-external-eeprom
https://www.hobbytronics.co.uk/eeprom-page-write

I heard often about memory pages but now finally I see it explained well. I tried the page wrap program and it writes perfectly to the Ram except writing back to serial monitor is not going so good. Maybe it goes too fast.

16Arduino Sensor help Empty Re: Arduino Sensor help Sun May 02, 2021 2:42 pm

arcachofo

arcachofo

@Mistral:

- Pullups needed in 0.4.14 is an error in this version about internal pullups in Arduino.

- About size of memory and I2C:
Memories with sizes upto 256 need address of only one byte.
For sizes > 256 you need 2 bytes.
So the code is different in the number of bytes you need to send to the I2C memory to tell the address you want write/read.


Though subject this. I tried this but is not working
https://www.arduino.cc/en/Reference/WireWrite
That is a generic code to send a byte to an I2C device.
For memories you need to follow some steps:
- Start transmission.
- Send address (1 or 2 bytes).
- Write data or read data.

If you are talking to other I2C devices the steps are different.

The letters in the ram-memory table do match up but not on the serial monitor?
I will have a look.


What I am trying to say is that it's a little bit difficult to find the address back because it does not show 4D0 but D0 in the left column.
Just solved that error in 0.4.15 branch Rev 176 and trunk Rev 268.

Thank for reporting!

EDIT:
Another one was that Simulide 4.15 crashed repeatedly yesterday when trying to open the memory table.
This issue is solved in 0.4.15 branch Rev 178 and trunk Rev 270.

As a workaround don't resize the memory after opening Memory Table.
Rezise it before opening Memory Table.

17Arduino Sensor help Empty Re: Arduino Sensor help Sun May 02, 2021 4:00 pm

feri



I did some tests and in fact the problem was the long address.
By reducing the ram capacity to 128 my code also worked, while for capacities greater than 255 you need the code posted by Arcachofo.
Writing works fine while reading with my code and memory 128 not from "2 3 4 5 6 etc ...."
but "2 3 141 5 142 6 143 etc ..." while the memory table is well written.
Thank you
greetings to all

Mistral likes this post

18Arduino Sensor help Empty Re: Arduino Sensor help Thu May 06, 2021 9:06 pm

feri



I did some tests with arduino and the attached scetch should write on serial monitor:
5
6
7
8

instead he writes in Simulide:
5
-1
-1
-1
it only reads the first address and not the next 3.
Greetings
Giordano
Attachments
Arduino Sensor help AttachmentI2CEEPROM.zip
You don't have permission to download attachments.
(1 Kb) Downloaded 1 times

19Arduino Sensor help Empty Re: Arduino Sensor help Thu May 06, 2021 11:55 pm

arcachofo

arcachofo

it only reads the first address and not the next 3.
Yes, looks like there is a problem...

And more people are having this problem:
https://simulide.forumotion.com/t34-using-i2c-with-arduino?nid=1#1024

I will have a look.

20Arduino Sensor help Empty Re: Arduino Sensor help Fri May 07, 2021 10:27 am

feri



From the tests I did to read the data from the slave, the master must send:

Master transmits
START -> 7 bite SLAVE ADRES + 0 ->
Slave responds
<- ACK
Master transmits
MEMORY ADDRESS ->
Slave responds
<- ACK
Master transmits
REPEAT START -> -> 7 bite SLAVE ADRES + 1 ->
Slave responds
<- ACK <- DATA (MA) . . . . . . . . [up to here it works ]
Master transmits
ACK -> . . . . . . . . . . . . . . . . . .  [here instead now I think it transmits   NACK -> STOP ]
Slave responds
<- ACK <- DATA (MA + 1)
Master transmits
ACK ->
Slave responds
<- ACK <- DATA (MA + 2)
Master transmits
ACK ->
Slave responds
<- ACK <- DATA (MA + 3)
Master transmits
ACK ->
Slave responds
<- ACK <- DATA (MA + n)
Master transmits
NACK -> STOP

Greetings
Giordano

21Arduino Sensor help Empty Re: Arduino Sensor help Fri May 07, 2021 4:07 pm

arcachofo

arcachofo

ACK -> . . . . . . . . . . . . . . . . . .  [here instead now I think it transmits   NACK -> STOP ]
Yes, there is the problem.
But it is not clear to me what is exactly happening.
looks like The software is indeed sending a stop condition:

STS 188, R24  

Where:
188 = TWCR
R24 = 213

So it is writting 213 to TWCR register:
213 = 1101 0101

TWIE  = 1
bit1 = 0
TWEN = 1
TWWC = 0
TWSTO = 1  <- Stop condition.
TWSTA = 0
TWEA = 1
TWINT = 1  <- Clear Interrupt flag.

22Arduino Sensor help Empty Re: Arduino Sensor help Fri May 07, 2021 7:37 pm

arcachofo

arcachofo

Ok, I found the problem.

Master is not sending ACK after reading the first byte, so wire is triggering an STOP Condition.

I'm working to solve it.

23Arduino Sensor help Empty Re: Arduino Sensor help Sat May 08, 2021 5:16 am

arcachofo

arcachofo

Problem should be solved in 0.4.15 branch Rev 186.
I will update trunk branch soon.

Sponsored content



Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum