Problem operating 74HCT4066 with Arduino providing logical states as enable pin - arduino

I made the necessary connections such as following ;
14 - Vcc
7 - Gnd
1 - A7
2 - Vcc
13 - 6
and the following is the code ;
const int analog_value_Z = A7;
const int enable = 7;
void setup() {
pinMode(enable, OUTPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(enable,HIGH);
Serial.print(analogRead(analog_value_Z));
Serial.print("\n");
delay(1000);
digitalWrite(enable,LOW);
Serial.print(analogRead(analog_value_Z));
Serial.print("\n");
delay(1000);
}
Since pin1 of switch is directly connected to analog read pin and the other pin is connected to Vcc, I am trying to switch between ON/OFF states by applying HIGH/LOW to enable pin. I expect result such as ;
1023
0
1023
0
.
.
.
Instead I get :
1023
1023
1023
.
.
What am I doing wrong ? This IC opens and closes the switch between Y and Z pins of it with the change in enable pin. Please provide some guidance.

Related

Arduino SPI clock phase synched with data for all 4 modes, what's wrong?

I'm trying to drive the LTC1664 DAC from an Arduino (Mega 2560). The SPI data and clock coming out of the Arduino is always synched (rise and falls) for all 4 modes when the DAC data sheet indicates phasing
I've tried all modes, the SS line on the chip is being brought low during the writes (as it should) and I've tried different speeds and shiftOut(), setClockDivider(), setDataMode(), beginTransaction() and endTransaction().
Is this a bug in Arduino SPI, something specific to the Mega 2560, should I try an Uno or Due? Help please! Code below o-scope traces.
BTW: The 16 bit word I'm trying to transmit is, 0x3600 (o-scope trace truncated).
/*
Test DAC Control
created 18 Mar 2020 (Covid-19, oppsies)
by Danny Holstein
*/
// inslude the SPI library:
#include <SPI.h>
void DAC(unsigned short value, unsigned char channel, int SS_Pin, int model);
enum models{LTC1664};
enum models model;
// set pin 10 as the slave select for the digital pot:
const int DAC_SS_Pin = 22;
void setup() {
// set the DAC_SS_Pin as an output:
// initialize SPI:
Serial.begin(115200);
SPI.begin();
Serial.println("SPI.begin");
pinMode(DAC_SS_Pin, OUTPUT);
}
void loop() {
// go through the six channels of the digital pot:
for (int channel = 1; channel < 6; channel++) {
delay(500);
DAC(128*channel, channel, DAC_SS_Pin, LTC1664);
}
delay(1000);
Serial.println("new loop");
}
/*
DAC Control
This function controls an LTC1664 Micropower Quad 10-Bit DAC.
The LTC1664 is SPI-controlled,and to command it, you send one 16 bit word,
A3 A2 A1 A0 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0 X1 X0
| ADDR | INPUT CODE | DON'T CARE
The circuit:
* CS - to digital pin 22 (SS pin)
* SDI - to digital pin 51 (MOSI pin)
* SDO - to digital pin 50 (MISO pin, not used in this function)
* CLK - to digital pin 52 (SCK pin)
created 18 Mar 2020 (Covid-19, oppsies)
by Danny Holstein
*/
void DAC(unsigned short value, unsigned char channel, int SS_Pin, int model) {
// take the SS pin low to select the chip:
digitalWrite(SS_Pin, LOW); delay(100);
unsigned short buf, b16;
unsigned char *c, b; c = (unsigned char *) &buf;
switch (model)
{
case LTC1664:
if (channel > 4) channel = 0xF;
buf = (channel << 12) | ((value & 0x3FF) << 2);
SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));
b16 = SPI.transfer16(buf);
Serial.print("0x" + String(buf, HEX)); Serial.println("\t0x" + String(b16, HEX) + "\t");
SPI.endTransaction();
break;
default:
break;
}
delay(100);
// take the SS pin high to de-select the chip:
digitalWrite(SS_Pin, HIGH);
// printf("value = 0x%04x", buf);
}
Turns out it wasn't related to the Arduino SPI function or phasing issues, the LTC1664 has a CLR pin that I had attached to a GPIO but had failed to command HIGH, it had been floating and inhibiting the chip, command HIGH and everything is good now.

Why 2nd PIR sensor is always HIGH?

I an getting a constant HIGH from 'inputPintwo' on the serial monitor. When 'inputPin' goes HIGH the relay is triggered and works properly because 'inputPintwo' is also HIGH (all the time).
I have a Very similar setup to: 2 PIR motion sensors +Arduino
I am not using pin 0 or 1 like the above answered question. I have replaced the sensor with a different one, in case it was bad hardware. I also unplugged the sensor and it still reads HIGH. The jumper is on retriggering on both sensors.
int ledPin = 13;
int inputPin = 2;
int inputPintwo = 4;
int pirState = LOW;
int val = 0;
int valtwo = 0;
#define RELAY1 7
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(inputPin, INPUT);
pinMode(inputPintwo, INPUT);
pinMode(RELAY1, OUTPUT);
Serial.begin(9600);
}
void loop(){
val = digitalRead(inputPin);
valtwo = digitalRead(inputPintwo);
if (val == HIGH && valtwo == HIGH) {
digitalWrite(ledPin, HIGH);
if (pirState == LOW) {
Serial.println("Motion detected!");
pirState = HIGH;
Serial.println("Light ON");
digitalWrite(RELAY1,1);
delay(500);
digitalWrite(RELAY1,0);
delay(500);
digitalWrite(RELAY1,1);
delay(500);
digitalWrite(RELAY1,0);
delay(500);
digitalWrite(RELAY1,1);
}
}
else {
digitalWrite(ledPin, LOW);
if (pirState == HIGH){
Serial.println("Motion ended!");
digitalWrite(RELAY1,0);
pirState = LOW;
Serial.println("Light OFF");
}
}
}
I expect both sensors to go HIGH only when motion is detected, which will cause the relay to go on and off several times, then stay on until the timer runs out on the sensors.
To identify the problem I recommend you to start with checking the hardware. You will need voltmeter/multimeter.
Double check if you are interfacing the sensor properly (check datasheet). Didn't you forget to connect e.g. the pull-down resistors?
Check power supply voltage on sensors – is the voltage within
manufacturer specifications?
Check breadboard connections if you are using one.
Check sensor output behaviour (voltage), if there is or is not a movement. Is the voltage constant or not? Constant voltage means that PIR sensor is NOT working properly. Before performing of this test disconnect output from Arduino input.
If everything seems OK or you do not have voltmeter, try to disconnect the PIR sensor and connect a wire between Arduino pin 4 and ground. Does digitalRead(inputPintwo) return LOW? If yes, you know that reading of the pin state works fine.
Below please see some recommendations related to your code:
use #define directive or static const int variable type to define Arduino pins as you do it with relay output pin RELAY1.
Example:
#define LED_PIN 13
#define INPUT_PIN 2
#define INPUT_PINTWO 4
or
static const int ledPin = 13;
static const int inputPin = 2;
static const int inputPintwo = 4;
In your case, where you are only interested in digital value (LOW/HIGH), use built pull-up resistor on the input pins. Thus the log. voltage level on the floating input pin is defined (HIGH). If you do not use pull-up resistors, voltage can be either log. 0 (LOW) or log. 1 (HIGH), what can lead to strange program/state machine behaviour
To activate pull-up resistors in the input pins, use
pinMode(inputPin, INPUT_PULLUP);
pinMode(inputPintwo, INPUT_PULLUP);

External EEPROM write/read problems (Arduino Uno)

I have Arduino Uno(Atmega328p) and external EEPROM connected via I2C.
My code in Arduino Studio:
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_MOSI 9
#define OLED_CLK 10
#define OLED_DC 11
#define OLED_CS 12
#define OLED_RESET 13
Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
#define EEPROM_ADDR 0x50
#include <M24AA01.h>
M24AA01 dev(0);
byte data[1] = {1};
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC);
writeEEPROM(1);
uint8_t readData = readEEPROM(1);
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(5,30);
display.print("read:");
display.print(data);
display.display();
}
void loop() {
// put your main code here, to run repeatedly:
}
void writeEEPROM(unsigned int eeaddress)
{
Wire.beginTransmission(EEPROM_ADDR);
Wire.write((int)(eeaddress >> 8)); // MSB
Wire.write((int)(eeaddress & 0xFF)); // LSB
Wire.write(data[0]);
Wire.endTransmission();
delay(5);
}
uint8_t readEEPROM(unsigned int eeaddress)
{
uint8_t rdata = 0xFF;
Wire.beginTransmission(EEPROM_ADDR);
Wire.write((int)(eeaddress >> 8)); // MSB
Wire.write((int)(eeaddress & 0xFF)); // LSB
Wire.endTransmission();
Wire.requestFrom(EEPROM_ADDR,1);
if (Wire.available()) rdata = Wire.read();
return rdata;
}
My physical connection of the EEPROM:
PIN 1: A0 - 5V(VCC)
PIN 2: A1 - 0(GND)
PIN 3: A2 - 0(GND)
PIN 4: VSS - 0(GND)
PIN 5: SDA - SDA
PIN 6: SCL - SCL
PIN 7: WP - 0(GND)
PIN 8: VCC - 5V(VCC)
I have a DIP version of EEPROM.
From the documentation I read that address of EEPROM is 0x50 (1010000). A0 is connected to 5V, but anyway A0,A1,A2 are "dont cares" as per documentation, so I believe address is fine.
No matter what I write to the EEPROM, in this case I just write 1 byte of data (number 1) - it reads 255.
One thing I noticed is that I do not specify in the code if I want to write or read (in documentation the 8th bit is read/write bit, read = 1, write = 0), but I do not know how to use it in code.

Arduino and ESP8266-01 communication issue

Fix the issue why complete code are not run and nothing are showed in WiFi setting like SSID.
Pin config:
USB to TTL Converter 3v3 - ESP8266 (VCC)
USB to TTL Converter GND - ESP8266 (GND)
Arduino pin 10 - ESP8266 (RX)
Arduino pin 11 - ESP8266 (TX)
Arduino GND - ESP8266 (CH_PD)
Now this is my code I want to connect the Android application to ESP module using WiFi after that I will be able to control relay on and off.
The code is here:
#include <SoftwareSerial.h>
#define DEBUG true
#define LED 40
#define two 42
#define three 44
#define four 46
SoftwareSerial esp8266(10,11); // make RX Arduino line is pin 2, make TX Arduino line is pin 3.
// This means that you need to connect the TX line from the esp to the Arduino's pin 2
// and the RX line from the esp to the Arduino's pin 3
void setup()
{
pinMode(LED,OUTPUT);
pinMode(two,OUTPUT);
pinMode(three,OUTPUT);
pinMode(four,OUTPUT);
Serial.begin(9600);
esp8266.begin(9600); // your esp's baud rate might be different
sendData("AT+RST\r\n",2000,DEBUG); // reset module
sendData("AT+CWMODE=2\r\n",1000,DEBUG); // configure as access point
sendData("AT+CIFSR\r\n",1000,DEBUG); // get ip address
sendData("AT+CIPMUX=1\r\n",1000,DEBUG); // configure for multiple connections
sendData("AT+CIPSERVER=1,80\r\n",1000,DEBUG); // turn on server on port 80
Serial.println("server started running on socket # 192.168.4.1:80");
}
void loop()
{
if(esp8266.available()) // check if the esp is sending a message
{
String cc="";
while(esp8266.available()){
// The esp has data so display its output to the serial window
char c = esp8266.read(); // read the next character.
Serial.write(c);
cc+=c;
}
Serial.print(cc);
delay(1000);
if(esp8266.find("+IPD,"))
{
delay(100);
int connectionId = esp8266.read()-48; // subtract 48 because the read() function returns
// the ASCII decimal value and 0 (the first decimal number) starts at 48
Serial.print("conn id = ");
Serial.println(connectionId);
String msg="";
while(esp8266.available()){
char c=esp8266.read();
msg+=c;
}
Serial.print("msg = ");
Serial.println(msg);
String vv=msg.substring(10,16);
Serial.print("vv = ");
Serial.println(vv);
vv.trim();
/////////////////////////////////////////////////////////
if(vv.length()>0){
if(vv=="sw1:on"){
Serial.println("switch 1 is turned on");
digitalWrite(LED,HIGH);
}
if(vv=="sw1:of"){
Serial.println("switch 1 is turned off");
digitalWrite(LED,LOW);
}
if(vv=="sw2:on"){
Serial.println("switch 2 is turned on");
digitalWrite(two,HIGH);
}
if(vv=="sw2:of"){
Serial.println("switch 2 is turned off");
digitalWrite(two,LOW);
}
if(vv=="sw3:on"){
Serial.println("switch 3 is turned on");
digitalWrite(three,HIGH);
}
if(vv=="sw3:of"){
Serial.println("switch 3 is turned off");
digitalWrite(three,LOW);
}
if(vv=="sw4:on"){
Serial.println("switch 4 is turned on");
digitalWrite(four,HIGH);
}
if(vv=="sw4:of"){
Serial.println("switch 4 is turned off");
digitalWrite(four,LOW);
}
if(vv=="sw_:on"){
Serial.println("all switches are turned on");
digitalWrite(LED,HIGH);
digitalWrite(two,HIGH);
digitalWrite(three,HIGH);
digitalWrite(four,HIGH);
}
if(vv=="sw_:of"){
Serial.println("all switches are turned off");
digitalWrite(LED,LOW);
digitalWrite(two,LOW);
digitalWrite(three,LOW);
digitalWrite(four,LOW);
}
}
///////////////////////////////////////////////////////////
String closeCommand = "AT+CIPCLOSE=";
closeCommand+=connectionId; // append connection id
closeCommand+="\r\n";
sendData(closeCommand,3000,DEBUG);
}
}
}
String sendData(String command, const int timeout, boolean debug)
{
String response = "";
Serial.print("command => ");
esp8266.print(command); // send the read character to the esp8266
long int time = millis();
while( (time+timeout) > millis())
{
while(esp8266.available())
{
// The esp has data so display its output to the serial window
char c = esp8266.read(); // read the next character.
response+=c;
}
}
if(debug)
{
Serial.print("response => ");
Serial.print(response);
}
return response;
}
You can try setting the baud rate to 115200 of the esp8266 since I have found that is what is mostly used. You have not specified if you can communicate at all.
You may try these...
Connect CH_PD with VCC then 3.3v
Serial.begin(115200);
ESP RESET to Arduino RESET

arduino program runs only at start up

My arduino program runs only when on start up or when i press the reset button, i dont know the problem as i am a new bee into this. Please need help.
i have some constants here for the remote control, and i am just trying to replicate what i am sending through the arduino ir led, every thing works fine except that the program runs only once.
#include <IRremote.h> // use the library
#define PanasonicAddress 0x4004 // Panasonic address (Pre data)
#define PanasonicPower 0x100BCBD // Panasonic Power button
#define PanasonicZero 0x1009899 // Panasonic button
#define PanasonicOne 0x1000809 // Panasonic button
#define PanasonicTwo 0x1008889 // Panasonic button
#define PanasonicThree 0x1004849 // Panasonic button
#define PanasonicFour 0x100C8C9 // Panasonic button
#define PanasonicFive 0x1002829 // Panasonic button
#define PanasonicSix 0x100A8A9 // Panasonic button
#define PanasonicSeven 0x1006869 // Panasonic button
#define PanasonicEight 0x100E8E9 // Panasonic button
#define PanasonicNine 0x1001819 // Panasonic button
#define PMute 0x1004C4D
#define PCPlus 0x1002C2D
#define PCMinus 0x100ACAD
#define PVMinus 0x1008485
#define PVPlus 0x1000405
#define PRTune 0x100ECED
#define JVCPower 0xC5E8
int receiverpin = 15; // pin 1 of IR receiver to Arduino digital pin 15
IRsend irsend;
IRrecv irrecv(receiverpin); // create instance of irrecv
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // start the IR receiver
// pinMode(7, OUTPUT);
for (int z = 11 ; z < 14 ; z++) // set up digital pins
{
pinMode(z, OUTPUT);
}
}
void loop()
{
if (irrecv.decode(&results)) // have we received an IR signal?
{
translateIR();
for (int z = 0 ; z < 2 ; z++) // ignore the repeated codes
{
irrecv.resume(); // receive the next value
}
}
}
void translateIR()
// takes action based on IR code received
// uses Sony IR codes
{
switch(results.value)
{
case PanasonicFour: pinOnTriple(1, 0, 0);
irsend.sendPanasonic(PanasonicAddress,PanasonicFour); // This should turn your TV on and off
break; // 2
case PanasonicTwo: pinOnTriple(0, 1, 0);
irsend.sendPanasonic(PanasonicAddress,PanasonicTwo);
break; // 3
case PanasonicFive: pinOnTriple(HIGH, LOW, HIGH);
irsend.sendPanasonic(PanasonicAddress,PanasonicFive);
break; // 5
case PanasonicSix: pinOnTriple(HIGH, HIGH, LOW);
irsend.sendPanasonic(PanasonicAddress,PanasonicSix);
break; // 6
case PanasonicSeven: pinOnTriple(HIGH, HIGH, HIGH);
irsend.sendPanasonic(PanasonicAddress,PanasonicSeven);
break; // 7
case PanasonicZero: pinOnTriple(LOW, LOW, LOW);
irsend.sendPanasonic(PanasonicAddress,PanasonicZero);
break; // 8
case PanasonicOne: pinOnTriple(LOW, LOW, HIGH);
irsend.sendPanasonic(PanasonicAddress,PanasonicOne);
break; // 9
case PanasonicThree: pinOnTriple(LOW, HIGH, HIGH);
irsend.sendPanasonic(PanasonicAddress,PanasonicThree);
break; // 10
case 0x100BCBD: pinOnTriple(LOW, LOW, LOW);
irsend.sendPanasonic(PanasonicAddress,PanasonicPower); // This should turn your TV on and off
irsend.sendJVC(JVCPower, 16,0); // hex value, 16 bits, no repeat
delay(50); // see http://www.sbprojects.com/knowledge/ir/jvc.php for information
irsend.sendJVC(JVCPower, 16,1); // hex value, 16 bits, repeat
delay(50);
break;
case 0x1004C4D:
irsend.sendPanasonic(PanasonicAddress,PMute);
break; // 11
case 0x1002C2D:
irsend.sendPanasonic(PanasonicAddress,PCPlus);
break; // 11
case 0x100ACAD:
irsend.sendPanasonic(PanasonicAddress,PCMinus);
break; // 11
case 0x1008485:
irsend.sendPanasonic(PanasonicAddress,PVMinus);
break; // 11
case 0x1000405:
irsend.sendPanasonic(PanasonicAddress,PVPlus);
break; // 11
case 0x100ECED:
irsend.sendPanasonic(PanasonicAddress,PRTune);
break; // 11
}
}
void pinOnTriple(int pin, int pino, int pini) // turns on digital pins for 1 second
{
digitalWrite(11, pin);
digitalWrite(12, pino);
digitalWrite(13, pini);
delay(1000);
digitalWrite(11, 0);
digitalWrite(12, 0);
digitalWrite(13, 0);
}
You should just check for receiving the repeat codes (0xFFFFFF) and just ignore this code. And just issue the resume command just once. You may be accidently skipping the valid codes.
Also, you need to issue a resume after transmitting IR (as tx disables rx).
This last one is probably the issue for you.
Hope it helps
may be worth putting in a delay before re-tx as the tx may overlap the repeat codes
For anyone interested in IR protocols - we have just launched a project for AnalysIR - IR Decoder & Analyzer GUI (Arduino & Raspberry Pi). Currently we support 17 IR protocols and are looking for more to add as part of the campaign. Suggestions Welcome!
If the project is successful, we hope to add support for Raspberry Pi !
You can find out more and support the Project by visiting http://igg.me/at/AnalysIR/x/3752156 or Screenshot via www.AnalysIR.com

Resources